Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
At what point does celery store result?
I am using celery + redis for tasks in Django web app. The problem is that I don't know at what point does celery store it's task result as JSON object into DB. Afterwards an issue is raised because it cannot store np array as JSON. import numpy as np from scipy.sparse import dok_matrix @shared_task(name="run_shortest_path_on_warehouse") def run_shortest_path_on_warehouse(adjacency_matrix): sparse_matrix = dok_matrix(adjacency_matrix) dist_matrix = dijkstra_algorithm(sparse_matrix).tolist() return {'optimal_distance_matrix': dist_matrix} Celery can't store the result, because of this exception: {"exc_type": "EncodeError", "exc_message": ["TypeError(\"Object of type 'ndarray' is not JSON serializable\",)"], "exc_module": "kombu.exceptions"} I know that numpy array isn't simply JSON serializable. That's why I used .tolist method in defining the dist_matrix variable. The question is, at what point does celery store it's variable and how can I store info from numpy array as task result? -
Django filter on date contained by any date range
I'm trying to filter a model which has a DateField (date) to retrieve a queryset of instances whose date is in any one of a list of DateRanges but I'm struggling to figure out the exact logic I need. So for example, if I have the following models:- class Period(models.Model): user = models.ForeignKey(User) range = DateRangeField() class Game(models.Model): date = models.DateField() and I've created 3 different date ranges, how do I get a list of all the Games whose date is in one of those 3 date ranges? I know I can iterate through the ranges and use a chained Q() filter for this but I need to put this all into an annotation on a large queryset which is going to have to use a Subquery so that won't work. My current effort looks like this:- periods = Period.objects.filter(user__id=OuterRef('id')).values_list('period', flat=True) games_in_periods = Game.objects.filter(date__contained_by=Subquery(periods)) but that doesn't work because the contained_by is being compared to a daterange but a queryset of dateranges. It feels like I'm close but I must have missed something silly. Any ideas? -
javascript string replace \" with \\"
Is there ANY way in javascript to make string '{"foo":"a \"b\" c"}' into '{"foo":"a \\"b\\" c"}'. I can not do it for the sake of my life. EDIT: I don't need any " replaced! only \"!! EDIT2: in browser console try JSON.parse('{"foo":"some \"quoted\" word"}') EDIT3: '{"foo":"some \"quoted\" word"}' is given by the server. EDIT4: '{"foo":"some \"quoted\" word"}' is INVALID JSON string in JS, though it's a VALID JS string. And '{"foo":"some \\"quoted\\" word"}' is VALID for both JS and JSON in JS. BUT it seems there is NO WAY in JS to turn '{"foo":"some \"quoted\" word"}' into '{"foo":"some \\"quoted\\" word"}'!!! EDIT5: in one directory I have: # test.py import json file = open('test.txt', 'w') file.write(json.dumps({'foo':'some "quoted" word'})) file.close() and // test.js var fs = require('fs') var data = JSON.parse(fs.readFileSync('./test.txt')) console.log(data) then I python test.py and node test.js. It logs {foo:'some "quoted" word'} no problem. SO Python and JavaScript understand JSON the same way. My question: Why it's not the same JSON when its server-client communication? And how to make it work? -
Django serializer's is_valid returning True even if validate returns False
I have a POST function like this: def post(self, request): try: serializer = CCTDSPostSerializer(data=request.data) print("serializer", serializer) print("is valid", serializer.is_valid()) The Serializer is as follows, its not a model serializer for specific reasons. class CCTDSPostSerializer(serializers.Serializer): status = serializers.CharField() transaction = serializers.CharField(allow_blank=True, allow_null=True) comment = serializers.CharField(allow_blank=True, allow_null=True) tds_id = serializers.ListField(child=serializers.IntegerField()) def check_tds_eligibility(self, data): tds_ids = data.get('tds_id', None) if tds_ids is not None: tds_obj = TDS.objects.filter(id__in=tds_ids, status='open') if tds_obj.count() == len(tds_ids): return tds_ids return None def validate_status_transaction(self, obj): status = obj.get('status', None) transaction = obj.get('transaction', None) if status == 'closed' and transaction is not None: return True elif status == 'rejected' and transaction is None: return True return False def validate(self, obj): validate_status_transaction = self.validate_status_transaction(obj) tds_ids = self.check_tds_eligibility(obj) if validate_status_transaction and tds_ids: print("returning obj") return obj print("returning false") return False The data that I am passing it is: { "tds_id":[1], "status":"closed", "transaction":"ABC", "comment":"Boom" } Now based on the conditions on the data present in the database, it comes to the statement print("returning false") i.e. it is returning False, but on the view side, the statement serializer.is_valid() gives the output as True How come the validate function returns False and the is_valid returns True? -
How to show %H:%M:%S' in Django Forms?
I have a form like this class QueryForm(forms.Form): enter_time = forms.TimeField(widget=forms.widgets.TimeInput(attrs={'type': 'time'})) This creates a form shown below in my template enter image description here How ever what I am looking for is something in this format: H:M:S My end goal is use this in my views.py as follows MyModel.objects.filter(created__lte=enter_time).values() I tried several ways to use a time picker,d only to fail. I really appreciate any kind of help. Thank You. -
Django Use Static files URL in Static Files
I want to use a custom font in my style.css file that will get served by StaticFiles I know I can hard code it with something like this: @font-face { font-family:"NotoSansArabic"; font-style:normal; src: url("/static/myApp/fonts/NotoSansArabicUI-ExtraBold.ttf") format("truetype") } Im looking for something like: {% static 'myApp/fonts/NotoSansArabicUI-ExtraBold.ttf' %} Is there other way to do this? both font and style.css are served as Static -
Error during redirecting large files in Django backend from Angular frontend to the Hadoop
I build web application with Angular 6 frontend, Django 1.11 backend and Hadoop 3.1. I need to send files of any size and format in the fastest possible way from Angular 6 frontend to the Hadoop via Django backend. My method in Django looks line shown below. Everything seems to be working fine with small files in different formats. However when I try to upload larger files I get the error shown at the bottom. Does anyone have an idea how can I solve this? Thanks in advance. def post(self, request): key = request.META.get('HTTP_AUTHORIZATION').split()[1] user_id = Token.objects.get(key=key).user_id user_name = User.objects.get(id=user_id).username upload_file(request.FILES['file'], user_name) url = 'http://192.168.0.12:9864/webhdfs/v1/user/' + str(user_name) + '/' + str(request.FILES['file']) + '?op=CREATE&user.name=myuser&createflag=&createparent=true&overwrite=false' return HttpResponseRedirect(url) -
Reached max children process limit in django?
I have a Django application in cpanel server, I got this error " Reached max children process limit: 6, extra: 2, current: 8, busy: 2, please increase LSAPI_CHILDREN. " What is this error? -
How can I sort by a DateTimeField and choose distinct elements based on a ForeignKey?
I am using PostgreSQL as database for my Django project. I have the following models: class Food(models.Model): """Stores a food entry""" name = models.CharField(max_length=50) image_id = models.CharField(max_length=20) description = models.TextField() class SurveyResult(models.Model): """Stores survey food result based on emoji and personality""" emoji = models.CharField(max_length=10) personality = models.CharField(max_length=50) food = models.ForeignKey(Food, on_delete=models.CASCADE) timestamp = models.DateTimeField(auto_now_add=True) rating = models.PositiveSmallIntegerField(default=0) I need to write a query sorted by '-timestamp', and just get distinct values for 'food'. This is my what I have tried: SurveyResult.objects.order_by('-timestamp').distinct('food') However, I am getting the following error: ProgrammingError: SELECT DISTINCT ON expressions must match initial ORDER BY expressions LINE 1: SELECT DISTINCT ON ("recommender_surveyresult"."food_id") "r... I have read several answers from several questions, and I found an answer that basically says to do something like this: SurveyResult.objects.order_by('food', '-timestamp').distinct('food') However, I am not getting what I want that is the latest SurveyResult objects that have a distinct food. How can I achieve this? -
Django Admin Filter List Filter based on User Name for non-admins
I am using the django admin side and i want to filter the list filter items with the info that is available only for the non admin user. He is not allowed to even see the rest of the users in the filter side. Can someone please help me to do this? I have tried in models and admin: def get_queryset(self, request): if not self.request.user.is_authenticated() or not self.request.user.is_superuser: user = request.user qs = super(ParkingModelAdmin, self).get_queryset(request) return qs.filter(user=request.user) In this situation the loged user was elise.cohen so she should see only her details out there. Thank you! -
GPIO control with AJAX and Django
So I encountered a problem I can't solve even having searched stack's resources and google. Let me share it with you. My config/tools are Raspberry Pi Zero W, Raspbian, python 3.5, Django framework, Bootstrap. I am communicating through and control GPIO on RPi via web browser (Chrome). My testbench is a set of LEDs which I am able to switch on and off using shell as well as using a straight-forward MVC (the circuit is correct). My goal and problem at the same time is to switch on and off the LEDs using AJAX so the page does not reload each time a button is pressed (what happens when I do not employ jquery/AJAX). At current stage after pressing a button it seems all the code is executed except of the function control_button in views.py. My logs after clicking a button: "GET /vendapp/ HTTP/1.1" 200 6027 "POST /vendapp/ HTTP/1.1" 200 6027 AJAX also gets to execute success: function... Here is the code: html 1 <!doctype html> {% load static %} <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <link rel="icon" href="../../../../favicon.ico"> <title>FP</title> <meta charset="utf-8" /> {% load staticfiles %} <link rel="stylesheet" href="{% … -
Django v2+ how to properly use AppConfig for reusable apps
After Django's 8 part tutorial there is an additional "Advanced Tutorial" which gives a quick-start like approach into python packaging. Namely, moving the Django tutorial app "polls" outside of a Django project and preparing it to be pushed to PyPi. While this information is useful and covers a bit more ground that Python's own packaging tutorial (e.g. the MANIFEST file to include static / template resources). It doesn't quite explain how to make an app reusable in the sense of configurable options. For example, if the app has a runtime dependency variable my_bool, how one would open up setting this option for their reusable app. My first instinct was AppConfig. However, subclassing AppConfig for this purpose isn't explained in the documentation. One could assume that all the options / settable variables come from the main settings.py file and set defaults if not there... but that also isn't explicitly stated or recommended in any of the Django docs that I have read thus far. For a reusable application I am making, I would like the user to be able to augment the app with their own data files. Thus they would need to supply these files in a directory somewhere (with … -
Django Form Work, but when i add div it doesnt post anymore
this code work flawlessly, i fill the form click send and the email is sent in no time <form role="form" action="" method="post" class="form-area contact-form text-right" id="myForm"> {% csrf_token %} <input type="text" name="contact_name" required id="id_contact_name" placeholder="Name: " /> <input type="email" name="contact_email" required id="id_contact_email" placeholder="Email: " /> <textarea placeholder="Message: " name="content" cols="40" rows="10" required id="id_content"></textarea> <button type="submit">Submit</button> </form> but when I add div and thing to make it look like i want, i fill the form, click send and nothing happen, cmd tell me mail.php 404 ej, this <form role="form" action="" method="post" class="form-area contact-form text-right" id="myForm"> {% csrf_token %} <span class="row"> <span class="col-lg-6 form-group"> <input class="common-input mb-20 form-control" id="form_contact_name" type="text" name="contact_name" required id="id_contact_name" placeholder="Name: " /> <input class="common-input mb-20 form-control" id="form_contact_email" type="email" name="contact_email" required id="id_contact_email" placeholder="Email: " /> </span> <span class="col-lg-6 form-group"> <textarea class="common-input mb-20 form-control" placeholder="Message: " name="content" cols="40" rows="10" required id="id_content"></textarea> </span> <span class="col-lg-12"> <button class="btn btn-outline-success" type="submit">Submit</button> </span> </span> </form> do you guys know why, at what i know div shouldnt make any problem when put into a form -
Django ModelForm not retrieving field's value
I'm using two ModelForms to create na user with a Profile instance. Profile ModelForm has a single field. I'm doing form.save(commit=False), setting the required field (country) other than the one in the form and saving. I get an error saying that 'country_id' cannot be null. models.py class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) country = models.ForeignKey(Country, on_delete=models.CASCADE) bio = models.TextField(blank=True) avatar = models.ImageField(upload_to='avatars/', null=True, blank=True) email_confirmed = models.BooleanField(default=False) @receiver(post_save, sender=User) def update_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) instance.profile.save() forms.py class UserRegisterForm(UserCreationForm): class Meta: model = User fields = ['username', 'email', 'password1', 'password2'] class ProfileRegisterForm(forms.ModelForm): class Meta: model = Profile fields = ['country'] views.py def register(request): if request.method == 'POST': user_form = UserRegisterForm(request.POST) profile_form = ProfileRegisterForm(request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save(commit=False) user.is_active = False profile = profile_form.save(commit=False) profile.user = user user.save() current_site = get_current_site(request) subject = 'Activate Your Account' message = render_to_string('account_activation_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(), 'token': account_activation_token.make_token(user) }) user.email_user(subject, message) return redirect('activation_email_sent') else: user_form = UserRegisterForm() profile_form = ProfileRegisterForm() return render(request, 'register.html', {'user_form': user_form, 'profile_form': profile_form}) -
Django 2.1.1 Import Errors
i use vscode on windows10. I just created a Django project+app like this: In the vscode powershell terminal: python -m venv venv #createing a virtual environment called venv .\scripts\activate #activate the virtual environment django-admin startproject api_order . #creates a django project called api_order with the folder structure python manage.py migrate #create database python manage.py runserver #starting the server because of the server running in my powershell terminal i take a second powershell terminal and continue with activating the venv and creating a app. .\scripts\activate python manage.py startapp api_order_app #creating a app called api_order_app When i now open the models.py python is reporting an problem "E0401:Unable to import 'django.db'" Same for all other imports in every other script. What did go wrong? I did this twice always the same problem. This is how it looks like in the IDE <img src="https://i.stack.imgur.com/JTmvb.jpg"> Thanks in Advance for helping me out. Ben When i now move to -
How can I populate a select field based on the choice of it's foreign key in Django?
Basically I have this situation. Models - Employer, EmployerContact EmployerContact is a foreignkey to Employer. In my form, I want to choose the employer, then dynamically populate the select field of EmployerContact with only contacts for the chosen Employer ONLY. Right now it shows all EmployerContacts from all Employers. I've found a couple of solutions but they seem sketchy... Could someone walk me through this? -
Django File Upload Ajax
I am trying to upload a file in Django using Ajax. I have tried different approaches but I've get same results. The file cannot be read in the view. Here is my codes: #JS $("#upload-file").click(function() { var data = new FormData($('#fileForm').get(0)); var csrftoken = getCookie('csrftoken'); $.ajax({ type: "POST", headers: {'X-CSRFToken': csrftoken}, url: "/countwords/", data: data, cache: false, processData: false, contentType: false, success: function(json) { var ss = json.len; alert(ss);}, error: function() { alert('error');} }); }); finally here is my view codes: if request.method == 'POST': print request.FILES.keys() print request.FILES print request.POST.keys() But I get following output when the code is run: [] MultiValueDict: {} [] -
How to change django template variable value in javascriptcode?
I've got a video on my html page. When a user click a button, the video begins. When the video ends, I need to change the users parameter 'views_num'. Here is my Javascript code: document.getElementById('get-views').onclick = function() { var videoE1 = document.getElementsByTagName('video')[0]; videoE1.play(); videoE1.addEventListener('ended', function () { { user.views_num| add:5}} }, false); }; So when I enter the html page and click the button, the video does not play automatically and I need to start it with the control button. Also after the video ends the 'views_num' does not change. How can I change the variable value? P.S. The views_num is an IntegerField. -
Add new field to model
I'm trying to add an external key to the model Article.New column: author. title=models.CharField(max_length=30); description=models.TextField(); date=models.DateTimeField(auto_now_add=True); author=models.ForeignKey(User,default=None,on_delete=models.PROTECT) Byt after migrate I got are error: OperationalError at /articles/ no such column: articles_article.author_id Article list: def article_list(requwest): articles=Article.objects.all().order_by('date') return render(requwest,"articles/article_list.html",{'articles':articles}) template: <div class="articles"> {% for article in articles %} <div class="article"> <p>{{article.snipped}}</p> <p>{{article.date}} </p> </div> {% endfor %} </div> I use this lesson:lesson -
how to display/insert the live video streaming from webcam in html page using python django framework?
I'm working in python django project to stream the live video from webcam def stream_video(request): return StreamingHttpResponse(stream_response_generator(),content_type="multipart/x-mixed-replace;boundary=frame") def stream_response_generator(): camera_port=0 ramp_frames=100 video_capture=cv2.VideoCapture(camera_port) #this makes a web cam object i=1 while True: ret, frame = video_capture.read() imgencode=cv2.imencode('.jpg',frame)[1] stringData=imgencode.tostring() yield (b'--frame\r\n' b'Content-Type: text/plain\r\n\r\n'+stringData+b'\r\n') i+=1 del(camera) and I have done these but Now i need to insert/display the streaming response in the html page.So please guide me to display the video streaming frame into html page as soon as possible.... Thanks in advance -
Wagtail CMS render image in template without templatetags
I need render more than 100 cards with image in the template. But I noticed that each render of a photo requires a query to the database. I upload pictures in low quality and I do not need to resize them using a template tag. Is there a way to render an image without using template tags? -
<Path> doesn't exist. Perhaps it was deleted? Django File upload
I am writing a small web server using Django and I got a huge problem. Whenever I upload an image (using Postman) the image gets uploaded properly but when I try to view it in the admin area it says doesn't exist. Perhaps it was deleted? Django File upload. Please help! I don't know what to try anymore. Here my urls.py file from django.urls import path, include from django.views.decorators.csrf import csrf_exempt from django.conf import settings from django.conf.urls.static import static from prealpha.views import preAlphaView from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('add', csrf_exempt(preAlphaView.addTrainingSession)) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ + static(settings.STATIC_URL, document_root=settings.STATIC_URL) and here the settings file: import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'omitted' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ 'prealpha.apps.PrealphaConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'colosseum_webserver.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', … -
Connecting django Query with Google search engine
I am trying to take a string query 'q' from user in my django website and do the following Do a google search on 'q'. take the first link after doing the google search on 'q', scrape some data from the link and return it on my website. Is there is any easy way to do the first step? I researched everywhere and couldn't find any way to do this. I am using python 3.5 with django 2.1 -
Getting "(CSRF token missing or incorrect.)" in android
I am getting CSRF token mismatch error on Django server while running below piece of code. Can some one help me identifying the issue here. try{ loginUrl = new URL(urls[0]); loginUrlConnection = (HttpURLConnection) loginUrl.openConnection(); loginUrlConnection.setRequestMethod("GET"); String userPass = "aniket" + ":" + "rinku123"; String basicAuth = "Basic " + Base64.encodeToString(userPass.getBytes(), Base64.DEFAULT); loginUrlConnection.setRequestProperty("Authorization", basicAuth); loginUrlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); loginUrlConnection.setRequestProperty("X-CSRF-TOKEN", "fetch"); loginUrlConnection.getContent(); if (HttpURLConnection.HTTP_OK == loginUrlConnection.getResponseCode()) { cookie = loginUrlConnection.getHeaderField("Set-Cookie"); String[] parts = cookie.split("\\=|\\;"); // split response by " and find the string that's 64 characters (csrf token) for(String s: parts) { if(s.length() == 64) { xcsrfToken = s; break; } } } loginUrl = new URL(urls[0]); loginUrlConnection = (HttpURLConnection) loginUrl.openConnection(); loginUrlConnection.setRequestMethod("POST"); loginUrlConnection.setRequestProperty("cookie", cookie); loginUrlConnection.setRequestProperty("X-CSRF-TOKEN", xcsrfToken); loginUrlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); loginUrlConnection.setConnectTimeout(10000); loginUrlConnection.setDoInput(true); loginUrlConnection.setDoOutput(true); loginUrlConnection.setUseCaches(true); loginUrlConnection.connect(); error :: Forbidden (CSRF token missing or incorrect.): /accounts/login/ -
Docker Compose + Postgres: Expose port
I am currently trying to use Docker for my new Django/Postgres project. I am working on a Mac and usually use Postico to quickly connect to my database. I used to connect like here: I used the official Docker documentation to setup docker-compose. I now have the issue, that I can't connect via Postico to the postgres db. It seems to me that the problem comes from the ports not being exposed. Anyone who can help me with that? version: '3' services: db: image: postgres web: build: . command: python3 manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - "8000:8000" depends_on: - db