Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Formset management_form gives wrong TOTAL_FORMS value after refreshing
I'm using Django 2.14 and my set up for this view is a form and a formset. The code looks like this: order_form = CreateOrder(instance=order, initial=initial) item_form_set = inlineformset_factory(Order, Item, form=AddItem, validate_min=True, min_num=1, extra=1) formset = ItemFormSet(instance=order, initial=items_initial) # form processing ... I used this setup to add more forms for the formset using javascript. So everything works fine if I try to add extra forms. The only issue is that I can't remove forms sometimes. This only happens after I add and remove some fields and refresh the page. The TOTAL_FORMS value is initialized as 1 even if there are two forms (1 initial + 1 extra). It seems like the browser is populating TOTAL_FORMS with the previous value (before the page is refreshed). <input type="hidden" name="item_set-TOTAL_FORMS" value="1" id="id_item_set-TOTAL_FORMS"> Based on this post from reddit it seems like one solution might be to add the autocomplete='off' tag to stop this behavior. I don't want to use an external library to solve this problem, so is there any other way to hook on to the formset management form to add these tags? Would it be any better to use javascript to modify the TOTAL_FORMS value? -
How to create a read-only custom Django model field?
Desired behavior: Excluded from save, create, update, etc. Can otherwise be accessed normally through the ORM, e.g. when loading model instances or querying. Included in DB creation SQL. Number 1 is definitely what I need the most help with, I think I've already got the rest working. Do I need to individually override all these methods? I'm hoping there's a common way I can exclude this field for any insert/update queries. For a bit more background, these are for MySQL generated fields. The fields are currently updated by various stored procs, so using something like signals wouldn't work; it has to happen outside of Python. Thanks! -
how to deploy a django-celery-rabbitmq-redis-flower app using Kubernetes
I have a Django-web app that uses celery workers to do asynchronous jobs. I have containerised the app and successfully ran it on my local machine and my local GCP account. However, when I deploy it using Kubernetes and create a webservice. I see connection errors in my logs such as [CRITICAL] WORKER TIMEOUT that I think comes from GUNICORN and ConnectionRefusedError: [Errno 111] Connection refused from flower. I don't know how to fix the problem. The followings summarise my deployment info: I have the following Dockerfile: FROM python:3.6 ENV LANG=C.UTF-8 LC_ALL=C.UTF-8 WORKDIR / COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY app/seg/requirements.txt ./app/seg/ RUN pip3 install --no-cache-dir -r ./app/seg/requirements.txt RUN rm requirements.txt RUN apt-get update RUN apt-get -y install ffmpeg RUN apt-get -y install nginx RUN mkdir -p config/nginx/conf.d RUN touch config/nginx/conf.d/local.conf COPY . / WORKDIR /app CMD gunicorn --preload -b :8000 api.wsgi and a docker-compose file as follows: services: app: build: . image: &app gcr.io/xxxx/app restart: "no" command: ["gunicorn", "--preload", "-b", ":8000", "api.wsgi"] env_file: &envfile - env.env volumes: - ./app:/app networks: - nginx_network depends_on: - broker - backend worker: build: . image: *app restart: "no" env_file: *envfile command: ["celery", "worker", "--app=worker.worker.app", "--concurrency=1", "--hostname=worker@%h", "--loglevel=ERROR"] volumes: - … -
Simple way to minify/scramble Django js
There is a simple way to minify and/or obfuscate JS that it's present in my django templates? I'm also using template variables and templates tag into js, so, the standard uglifyjs or similar are not fitting my original request. Anything to do? -
Django first run: SyntaxError: invalid syntax
https://docs.djangoproject.com/en/2.1/intro/tutorial01/ Hi, I just installed django and I'm running through the starter tutorials and when I enter the first command: $ python -m django --version I get an error stating: SyntaxError: invalid syntax. Why am I getting this? I am brand new to django. -
Setting django timezone on runtime
While I have a default timezone of UTC in my settings file, occasionally I need to overwrite this timezone. Without using something like pytz is it possible to set the tza at runtime. For example: from django.utils import timezone tz = 'America/New_York' now = timezone(tz).now() # in pseudocode Would there be a way to accomplish the above? -
How to pass 'using' DB to the django connection object
To query a specific database in django I can do: Item.objects.using('specific_db').all() Is there a way to do the same using a django connection? For example: >>> from django.db import connection >>> cursor=connection.using('specific_id').cursor() If not, how could I get a cursor/connection for a specific DB without manually providing all the credentials? -
installing APACHE MacOS: configure: error: C compiler cannot create executables
I am trying to install APACHE webserver on my Macbook Pro for use with DJango and everything seems to go fine with the install until: checking whether the C compiler works... no configure: error: in `/Users/darrinwebster/desktop/httpd-2.4.37': configure: error: C compiler cannot create executables whereis gcc gives: /usr/bin/gcc gcc-v gives: Configured with:--prefix=/Volumes/Blackbox/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin15.6.0 Thread model: posix Here is the config log: This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. It was created by configure, which was generated by GNU Autoconf 2.69. Invocation command line was $ ./configure --prefix=/Darin-Macbook-Pro/local/apache2 --------- Platform. --------- hostname = Darrins-MacBook-Pro.local uname -m = x86_64 uname -r = 15.6.0 uname -s = Darwin uname -v = Darwin Kernel Version 15.6.0: Thu Jun 21 20:07:40 PDT 2018; root:xnu-3248.73.11~1/RELEASE_X86_64 /usr/bin/uname -p = i386 /bin/uname -X = unknown /bin/arch = unknown /usr/bin/arch -k = unknown /usr/convex/getsysinfo = unknown /usr/bin/hostinfo = Mach kernel version: Darwin Kernel Version 15.6.0: Thu Jun 21 20:07:40 PDT 2018; root:xnu-3248.73.11~1/RELEASE_X86_64 Kernel configured for up to 4 processors. 2 processors are physically available. 4 processors are logically available. Processor type: i486 (Intel 80486) Processors active: 0 … -
Mac osx Mojave compatibility issue with mod_wsgi for use with Django and apache
I am setting up a new Mac server running Mojave 10.14.2 to run with django and which utilizes mod_wsgi for Apache. When adding the configuration to load the mod_wsgi module, Apple does not allow for third party modules with their Apache installation. I have this working on a windows PC but need to put on the Mac server. I have tried a few things as posted HERE I have also tried a custom installation of httpd through homebrew and it does not work. -
Django template slow when creating report (many variables, many models, many lists)
I'm a newbie trying to use Django to create a report about the participants of a team and I'm doing something fundamentally wrong and have spent several hours trying to figure out what's going wrong - the report is still hitting the DB about 4000+ times. An abbreviated version of what I'm doing is below. ​ Any help, pointers or other resources would be much appreciated! ​ For the Team model: class Team(BenchmarkData): year = models.ForeignKey(Period, on_delete=models.CASCADE, null=True) sponsor = models.ForeignKey(Person, on_delete=models.CASCADE) goal = models.ForeignKey(Goal, on_delete=models.CASCADE, null=True) ...other non-relational properties... def _participation_queryset(self): from .participation import Participation pp = Participation.objects.filter(team=self.id) return pp @cached_property def average_points(self): list_participation = [participation.calculated_yearly_points_per_hour for participation in self._participation_queryset()] try: return mean(list_participation) @cached_property ... For the Participation model: class Participation(models.Model): person = models.ForeignKey(Person, on_delete=models.CASCADE, null=True) team = models.ForeignKey(Team, on_delete=models.CASCADE, null=True) year = models.ForeignKey(Period, on_delete=models.CASCADE, null=True) start_date = models.DateField('Began', null=True) end_date = models.DateField('Left', blank=True, null=True) active = models.BooleanField('Active?', null=True) yearly_points = models.FloatField(default=0, null=True) yearly_hours_of_play = models.FloatField(default=0) ...other non-relational properties... @cached_property def calculated_yearly_points_per_hour(self): try: return self.yearly_points / self.yearly_hours_of_play except ZeroDivisionError: return 0 ...other cached properties... For my View, I used: class PlanReport(TemplateView): template_name = 'pension/report/index.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) self.team = get_object_or_404(Plan, id=self.kwargs['team_id']) self.sponsor = get_object_or_404(Company, id=self.kwargs['sponsor_id']) self.report … -
debug toolbar django not executing
I've been trying to use the debug Toolbar from django by applying the docs. Before adding the INTERNAL_IPS=['127.0.0.1'] on settings.py, django can be executed locally and i can run it, but the toolbar doesn't appear. Once I add the INTERNAL_IPS=['127.0.0.1'], I get the following error: Internal Server Error: / Traceback (most recent call last): File "C:\Anaconda3\envs\mydjan\lib\site-packages\django\core\handlers\exception.py", line 35, in inner response = get_response(request) File "C:\Anaconda3\envs\mydjan\lib\site-packages\django\utils\deprecation.py", line 97, in __call__ response = self.process_response(request, response) File "C:\Anaconda3\envs\mydjan\lib\site-packages\debug_toolbar\middleware.py", line 133, in process_response panel.generate_stats(request, response) File "C:\Anaconda3\envs\mydjan\lib\site-packages\debug_toolbar\panels\staticfiles.py", line 136, in generate_stats "staticfiles_finders": self.get_staticfiles_finders(), File "C:\Anaconda3\envs\mydjan\lib\site-packages\debug_toolbar\panels\staticfiles.py", line 148, in get_staticfiles_finders for path, finder_storage in finder.list([]): File "C:\Anaconda3\envs\mydjan\lib\site-packages\django\contrib\staticfiles\finders.py", line 125, in list for path in utils.get_files(storage, ignore_patterns): File "C:\Anaconda3\envs\mydjan\lib\site-packages\django\contrib\staticfiles\utils.py", line 28, in get_files directories, files = storage.listdir(location) File "C:\Anaconda3\envs\mydjan\lib\site-packages\django\core\files\storage.py", line 313, in listdir for entry in os.listdir(path): FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\Alvaro Lloret\\Home\\alpha_trader\\static\\' [10/Jan/2019 20:14:30] "GET / HTTP/1.1" 500 94293 Find hereby my settings.py: import os from django.contrib.messages import constants as messages # 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.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'n2th-ublablalablablals' … -
How do I reference a user's most recently uploaded file in a view, then do something with the file, then return the data?
I'm trying to build a site specific to a product that I make. The product records data from sensors on a CSV file. What I want to do: Users can create an account, upload their CSVs, then, using chart.js, I pass x and y axis data to the template through template tags. I have the idea working well with a local CSV test file, but now I need to reference the users specific files in the view, then parse the csv and return the data as context, but I'm stuck on how to reference the specific users csv and how to handle the file in the view. models.py from django.db import models from django.contrib.auth.models import User from django.contrib.auth import get_user_model def user_directory_path(instance, filename): return 'user_{0}/{1}'.format(instance.user.id, filename) class Document(models.Model): docfile = models.FileField(null=True, blank=True, upload_to=user_directory_path) description = models.CharField(max_length=255) user = models.ForeignKey(User, on_delete=models.CASCADE) def get_upload_path(self,filename): return "media/uploads/"+str(self.user.id)+"/"+filename def __str__(self): return self.description class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) #additional portfolio_site = models.URLField(blank=True) profile_pic = models.ImageField(upload_to='profile_pics',blank=True) docfiles = models.OneToOneField(Document, on_delete=models.CASCADE) def __str__(self): return self.user.username views.py from django.shortcuts import render, redirect, render_to_response, reverse from django.http import HttpResponse from django.http import HttpResponseRedirect from django.conf import settings from django.contrib import messages from . import parsecsv import json from … -
How Django and JavaScript work together for running Python script?
I am wondering how to accomplish the below with Django: Consider a function in JavaScript: $('#button').click(function(){ var rand_val = Math.random(); // here some code sends this rand_val to a Python script in Django app // as seen below, script is run and generates result alert('This is the result ' + result); }) The script in the Django app: def my_func(rand_val): # result = (some operations with rand_val) return result The Python script will be run in Django virtual environment. With a button click, run a Python script and show the result on the page. -
Interfacing a QR code recognition to a django database
I'm coming to you with the following issue: I have a bunch of physical boxes onto which I still stick QR codes generated using a python module named qrcode. In a nutshell, what I would like to do is everytime someone wants to take the object contained in a box, he scans the qr code with his phone, then takes it and put it back when he is done, not forgetting to scan the QR code again. Pretty simple, isn't it? I already have a django table containing all my objects. Now my question is related to the design. I suspect the easiest way to achieve that is to have a POST request link in the QR code which will create a new entry in a table with the name of the object that has been picked or put back, the time (I would like to store this information). If that's the correct way to do, how would you approach it? I'm not too sure I see how to make a POST request with a QR code. Would you have any idea? Thanks. PS: Another alternative I can think of would be to a link in the QR code to … -
Django: Successive Queryset Filtering Stops Filtering after First Query
The debug session below illustrates what I am trying to say. I have executed this under the Django dev server, Cherrypy server and Werkzeug debugger. Using Django 1.11.10 on Windows 10, MySQL 5.5 back end. The function (at the end of this post) does successive filtering on a QuerySet. I stopped execution to perform some sanity checks. What I found is that the first filter operation worked event_values.count() == 14 but any further attempts to filter would always yield the same results. For example, I filtered on a pk=72 and still all 14 model instances were returned. I tried to use get(pk=72) and got a MultipleObjectsReturned exception. Interestingly, when I perform the successive filtering in the Django shell, I get the results that I am supposed to get. Is this a bug in Django? Am I doing something wrong? Thanks in advance. (Pdb) l 160 event_values = event_values.filter( 161 tsn__gte=tsn_start, tsn__lte=tsn_end) 162 163 import pdb; pdb.set_trace() 164 165 -> if tso_start or tso_end: 166 tso_start, tso_end = resolve_ranges(tso_start, tso_end) 167 168 event_values = event_values.filter( 169 tso__gte=tso_start, tso__lte=tso_end) 170 (Pdb) tsn_start 5800 (Pdb) tsn_end 6000 (Pdb) event_values.count() 14 (Pdb) event_values <QuerySet [<ComplianceEvent: pk: 72 -- 383201 -- 2017-12-11 -- SB>, <ComplianceEvent: … -
How do I import constants from my settings.py file into my Django console?
I'm using Python 3.7 with Django. I have this at the end of my settings.py file MAX_AGE_IN_HOURS = 18 When I fire up my console, how do I get this constant recognized? I have tried teh below, but get an error complaining that the above is not defined. from django.conf import settings (age.seconds / 3600) > MAX_AGE_IN_HOURS Traceback (most recent call last): File "<input>", line 1, in <module> NameError: name 'MAX_AGE_IN_HOURS' is not defined -
Nginx upstream error when trying to upload from cache using Django
I have a web application where one of the features is uploading a file into a specific system. The file is uploaded to a remote API by Django through an Ajax call using the requests python module. This works fine when I run the app locally but when I run it through Nginx I always get the following error when I try to upload the file [error] 22665#0: *254004 upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx.xx.xx, server: redacted.com, request: "POST /report/ajax/upload_file/ HTTP/2.0", upstream: "http://unix:/var/www/webapp.sock:/report/ajax/upload_file/", host: "redacted.com", referrer: "https://redacted.com/report/key/sha256/" And a 502 error in the developer tools POST https://redacted.com/report/ajax/upload_file/ 502 The client side code which makes the ajax call is function handleUploadButton() { $('#upload-button').on('click', function() { var data = new FormData(); var file = $('form input[type=file]')[0].files[0]; data.append('file',file); data.append('sha256', sha256); data.append('api_key', 'key'); console.log(file); $.ajax({ url: '/report/ajax/upload_file/', data: data, processData: false, contentType: false, type: 'POST', success: function(data) { $('#sandboxMessage').text(data.res); $('#uploadResults').slideDown(300); }, error: function(data) { $('#customMessage').text('Sample upload failed'); $('#uploadResults').slideDown(300); }, }); }); } And the server side python code is def upload_file(request): sha256 = request.POST.get('sha256', None) api_key = request.POST.get('api_key', None) uploadFile = request.FILES['file'] headers = {'X-TOKEN-KEY': api_key} res = requests.put(url='https://redacted.com/v1-1/file/' + sha256, headers=headers, data=uploadFile) data = { 'sha256': sha256, … -
Django Rest Framework for network device configuration
I have a network device (switch) with a REST API. Now, I want to create a Django application that can access this API and configures the device. Does it make sense to use the Django Rest Framework for this application? -
How to resolve ERR_CONNECTION_REFUSED when connecting to virtualenv/django running on a server
I have installed virtualenv and Django in my user section on a ubuntu server. Everything appeared to install correctly and when I start the server with python manage.py runserver I get the following message, which seems correct: Performing system checks... System check identified no issues (0 silenced). You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. January 10, 2019 - 16:38:32 Django version 2.1.5, using settings 'form01.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. The problem is when I go to http://127.0.0.1:8000/ in my browser I get an ERR_CONNECTION_REFUSED instead of the Django install success page. I expect to see the Django successful install screen at http://127.0.0.1:8000 but instead get ERR_CONNECTION_REFUSED. -
Quickly get query set of objects related by Foreign key
Here are my models: class A(models.Model): #Some fields class B(models.Model): #Some fields class C(models.Model): a = models.ForeignKey(A) b = models.ForeignKey(B) #Some more fields class Meta: unique_together = ['a', 'b'] Now based on some object of A (say 'a') , I want set of all 'b' for which C.a = 'a' and C.b = 'b' Currently the way I am doing this is: set_c = C.objects.filter(a='a') set_b = [c.b for c in set_c] First of all this gives me a list (a queryset would be preferred). Second, I am concerned about line 2 having list comprehension , as it may take a lot of time for huge data. Is there a better way to achieve this? -
Foreign key feild with to_feild constraint gives duplicate keys error
I have a foreign key to another model with a to_field constraints with points to a field in the model, but when I try to run migrate it gives me this error: django.db.utils.IntegrityError: could not create unique index "disctest_respondent_least_disc_ce7da5e0_uniq" DETAIL: Key (least_disc)=(4,9,1,4,6) is duplicated. Here is the Model: class Combined(BaseRespondent): least_disc = models.ForeignKey(Respondent, to_field='least_disc', related_name="combined_least_disc", on_delete=models.CASCADE) most_disc = models.ForeignKey(Respondent, to_field='most_disc', related_name="combined_most_disc", on_delete=models.CASCADE) and here is the reference model to the foreign key class Respondent(BaseRespondent): least_disc = models.CharField(max_length=256, default='6,0,7,7,4' ,unique=True) most_disc = models.CharField(max_length=256, default='7,5,1,4,7', unique=True) most_personality = models.ForeignKey(Personality, on_delete=models.SET_NULL, null=True, related_name="most_personality") least_personality = models.ForeignKey(Personality, on_delete=models.SET_NULL, null=True, related_name="least_personality") here is also the error I get: django.db.utils.IntegrityError: could not create unique index "disctest_respondent_least_disc_ce7da5e0_uniq" DETAIL: Key (least_disc)=(4,9,1,4,6) is duplicated. How do I go about solving this issue? Thanks in advance for your prompt response. -
Query all database entries for the first day of the month
I am currently trying to use the Django query framework to query the first entry of each month from a database with lots of entries over many years. Is this somehow possible? Suppose I have a database with one entry per day (for some days there might not be an entry). What is an efficient way to query the first entry for each month (or each week) for each of the years. I hop you can help me. Thank you so much in advance! -
Migrate User Passwords from One DB to Another
I'm currently using mongoDB as my application DB and hashing passwords with bcrypt. I'm going to be moving over to a Django application as well as a PostgreSQL DB and will be transferring all my users over to that, and using Django's built in authentication system. I'm not sure how to handle the passwords, however. Is it possible to move users over without requiring all users to reset their passwords? -
django: how to define apps
when I try to define any app in django it always make django server stop (127.0.0.1:8000) and it show me error in cmd like this File "C:\Users\SELL\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 953, in _find_and_load_unlocked File "", line 219, in _call_with_frames_removed File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 965, in _find_and_load_unlocked ModuleNotFoundError: No module named 'homedjango' I just define app by going to setting.py and installed_apps [] and add 'myAppName.apps.MyAppNameConfig', is this right and if it why does server stop and show me errors thank you all. -
Cannot make Python 3.7 work in Ubuntu 18.04
I am learning Python and Django in Ubuntu 18.04 where both Python 3.6.7 and 3.7.2 is installed. Whenever Pyhon3 command is given its showing the prompt of 3.7, but when I use pipenv to create a virtual environment for a project it's still using /usr/bin/python3(Python3.6.7) and not of Python3.7.2. Even Python3.7.2 has been configured as default with highest priority of 2 by using the command: sudo update-alternatives --install /usr/binpython3 python3 /usr/bin /python3.6 1 sudo update-alternatives --install /usr/binpython3 python3 /usr/bin /python3.7 2 sudo update-alternatives --configure python3 and the above command shows the Python3.7 as the default by a "*" symbol, but still pipenv uses Python3.6.7. I want pipenv to use only Python3.7 and 3.6. Don't know what to do. Please help.