Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django integration with Charts.js, I'm unable to find the syntax for passing the data to the template
I'm trying to integrate charts.js charts with django. So far im able to display the pie chart. But the problem im facing is in the bar chart or charts such as where i have not just x and y but x, y1, y2, y3 etc. I'm unable to find the syntax for passing the data to the template Here's my Model class due(models.Model): months= models.CharField(max_length=30) paid = models.PositiveIntegerField() unpaid = models.PositiveIntegerField() def __str__(self): return "{}-{}".format(self.months, self.paid,self.unpaid) Here's my View def graph2(request): labels = [] data = [] queryset = due.objects.order_by('-paid')[:10] for Due in queryset: labels.append(Due.name) data.append(Due.paid,Due.unpaid) return render(request, 'account/graph1.html', { 'labels': labels, 'data': data, }) And this is where i want to access the data from my view, how do i replace the both data fields with my paid and unpaid fields from views. I know the data: {{ data|safe }} this is the syntax but it gets only one column. How do i get y1, y2, y3 etc. <script> new Chart(document.getElementById("bar-chart-grouped"), { type: 'bar', data: { labels: ["1900", "1950", "1999", "2050"], datasets: [ { label: "Africa", backgroundColor: "#3e95cd", data: [133,221,783,2478] }, { label: "Europe", backgroundColor: "#8e5ea2", data: [408,547,675,734] } ] }, options: { title: { display: true, text: 'Population … -
Why does the for loop behave like this?
When I run the program, the for loop does not delete all the list items, but only certain ones. Why? Here is the code: def res(array): for i in range(len(array)): print(array[i]) del array[i] print(array) arr = [10, 5, 40, 30, 20, 50] res(arr) -
didn't return an HttpResponse
Good morning, when I try to send the form, I get the error, and when I send it, it generates that my view does not return any httpresponse object. this is the view class ProductView(View): template_name = 'products/product.html' model = Product form_class = ProductForm def get_queryset(self): return self.model.objects.filter(state=True) def get_context_data(self, **kwargs): context = {} context['product'] = self.get_queryset() context['list_product'] = self.form_class return context def get(self, request, *args, **kwargs): return render(request, self.template_name, self.get_context_data()) def post(self, request, *args, **kwargs): list_product = self.form_class(request.POST) if list_product.is_valid(): list_product.save() return redirect('products:product') and this is the form class ProductForm(forms.ModelForm): name_product = forms.CharField( max_length=25, widget=forms.TextInput( attrs={ 'class': 'form-control', 'id': 'name_product', } ) ) def clean_name_product(self): name_product = self.cleaned_data.get('name_product') if Product.objects.filter(name_product=name_product).exists(): raise forms.ValidationError('El nombre del producto ya existe') return name_product class Meta: model = Product fields = ( 'name_product', 'description', 'price', 'category', 'state', 'image' ) labels = { 'name_product': 'Nombre del Producto', 'description': 'Descripcion', 'price': 'Precio', 'category': 'Categoria', 'state': 'Estado', 'image': 'Imagen del Producto', } widgets = { 'name_product': forms.TextInput( attrs={ 'class': 'form-control', 'id': 'name_product', } ), 'description': forms.TextInput( attrs={ 'class': 'form-control', 'id': 'description', } ), 'price': forms.NumberInput( attrs={ 'class': 'form-control', 'id': 'price', } ), 'category': forms.SelectMultiple( attrs={ 'class': 'custom-select', 'id': 'category', } ), 'state': forms.CheckboxInput(), } when I give … -
How to manage version controlled Django migrations as a team?
We're a small team of developers working on a version-controlled Django 3 project. We are worried about utilizing migrations and the possibility of overwriting one another's migration files. Options we've considered: Using the --name / -n option of makemigrations to have a naming convention, but that seems cumbersome Django documentation mentions tools for version control, but I don't see much on the specifics on it. How do other teams handle this? -
Can not get images to be displayed in Django template
The title is pretty much self explanatory. I did put some images in the static/rakunai folder and added the dirs etc, the whole shebang. I still can't get to display it. I am using docker. Python 3.8 was used and the newest django version was used. The settings.py: ``` """ Django settings for rakunai project. Generated by 'django-admin startproject' using Django 2.2.12. For more information on this file, see https://docs.djangoproject.com/en/2.2/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.2/ref/settings/ """ 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.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ '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 = 'rakunai.urls' STYLESHEETS='/stylesheets/' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ['templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'rakunai.wsgi.application' # Database # https://docs.djangoproject.com/en/2.2/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', … -
What is a "state operation" and "database operation" in SeparateDatabaseAndState?
This is an extension to this question: Move models between Django (1.8) apps with required ForeignKey references The Nostalg.io's answer worked perfectly. But I still can't get what is a "state operation" and "database operation" and what actually is going on when using SeparateDatabaseAndState. -
OSError: [WinError 123] La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte: '<frozen importlib._bootstrap>'
I have an error but did know why and how to resolve I start a new projects and use code of my previous project for authentification I install all dependencies Problem come form 2 dependencies crispy_forms and boostrap4 I already use in the same way As you can see in the settings.py below, crispy_forms and boostrap4 are declared in apps My virtual env is active and I have install all dependencies in this environnement If I deactivate the 'crispy_forms' and 'bootstrap4' in INSTALLED_APP, my home page can be display but error raised if I try to display the login page (error: 'crispy_forms_tags' is not a registered tag library) settings.py """ Django settings for mereva project. Generated by 'django-admin startproject' using Django 3.0.6. For more information on this file, see https://docs.djangoproject.com/en/3.0/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.0/ref/settings/ """ 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/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'rc_oh(6&d$)jin3txiiiqvuw2+-mla*f^em9!=yzlgx_o^mph9' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # … -
Speed up Django Migrations on Large Database
I have a MySQL DB with multiple tables that each have almost 2M records. When I run a migration that updates those tables, it's very slow. I tried beefing up the server to 64 CPU and 240GB RAM (probably really unnecessary) but it's still taking too long. Is there any way to speed up the migrations? -
create a table in django and save model to it
I am stuck on this stage where I need to save my model to a particular table. I have installed postgres and now I want to firstly create 2 tables A and B and append instance of model 'modelA' to table 'A' and of 'modelB' to B. I cannot find much on this on googling and stackoverflow in a clear form. -
Django always trying to connect to Mysql using root user
I created a Dockerfile to build a docker image for a django Project : FROM python:3.6 RUN apt-get update \ && apt-get install -y --no-install-recommends \ postgresql-client \ && rm -rf /var/lib/apt/lists/* WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install -r requirements.txt COPY shop /usr/src/app RUN python manage.py migrate EXPOSE 8000 CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] And in my settings.py i have this : DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'django', 'USER ': 'admin', 'HOST': 'mydatabase.eu-west-3.rds.amazonaws.com', 'PASSWORD': 'adminadmin', 'PORT': '3306', } } Now when building the image i'm having this error : ** django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'172.31.7.119' (using password: YES)") ** What don't understand is why the error talking about a user "root" while in my settings.py it's clearly set to "admin" ? -
AttributeError: module 'django.db.models' has no attribute 'RegexField'. python (django)
I want add RegexField but i have this error. why? found it on Google but there is nothing on regexfield this is error mob = models.RegexField(regex=r'^+?1?\d{9,15}$') AttributeError: module 'django.db.models' has no attribute 'RegexField' models.py from django.contrib.auth.models import AbstractUser, BaseUserManager from django.db import models from django.utils.translation import ugettext_lazy as _ from django import forms class UserManager(BaseUserManager): """Define a model manager for User model with no username field.""" use_in_migrations = True def _create_user(self, email, password, **extra_fields): """Create and save a User with the given email and password.""" if not email: raise ValueError('The given email must be set') email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, email, password=None, **extra_fields): """Create and save a regular User with the given email and password.""" extra_fields.setdefault('is_staff', False) extra_fields.setdefault('is_superuser', False) return self._create_user(email, password, **extra_fields) def create_superuser(self, email, password, **extra_fields): """Create and save a SuperUser with the given email and password.""" extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) if extra_fields.get('is_staff') is not True: raise ValueError('Superuser must have is_staff=True.') if extra_fields.get('is_superuser') is not True: raise ValueError('Superuser must have is_superuser=True.') return self._create_user(email, password, **extra_fields) class User(AbstractUser): """User model.""" username = None email = models.EmailField(_('email address'), unique=True) mob = models.RegexField(regex=r'^\+?1?\d{9,15}$') USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] objects = UserManager() -
Duplicate elements (products) is creating in Swiper function of js in django template
I am trying to show some products Django template. I have tried Swiper js class to show those products in a div. There are two buttons "prev" and "next" used to scroll horizontally in those products. In my Product model, there is one product i have inserted from admin and going to see how its shown in the Swiper div. But its showing duplicates of that one element whenever i am trying to click "prev" or "next" or grabbing by "Cursor" I got idea from this link. Picture in which my product is shown in the Swiper div: HTML CODE in Django template: <!-- products --> <div class="container my-4 bg-white border border-light-dark flex"> <div class="lunchbox"> <!-- slider main container --> <div id="swiper1" class="swiper-container"> <!-- additional required wrapper --> <div class="swiper-wrapper"> <!-- slides --> {% for product in products %} <div class="swiper-slide"> <div class="product"> <img class="photograph" src="/media/product_images/{{product.product_code}}.jpg" alt=""> <h2 class="product__name">{{product.name}}</h2> <p class="product__description"><span class="font-weight-normal">৳</span>{{product.product_price}}</p> </div> </div> {% endfor %} </div> <!-- pagination --> <div class="swiper-pagination"></div> </div> <!-- navigation buttons --> <div id="js-prev1" class="swiper-button-prev btn-edit" style="top:35%;"></div> <div id="js-next1" class="swiper-button-next btn-edit" style="top:35%;"></div> </div> </div> JavaScript CODE: (function() { 'use strict'; const mySwiper = new Swiper ('#swiper1', { loop: true, slidesPerView: 'auto', centeredSlides: true, a11y: … -
Django unit tests are returning a strange format where strings are being returned in the format [33 chars] of [num chars]
I am currently building a django-rest-framework API and the unit tests seem to be mostly working but there seems to be a problem with how strings are being handled. So for example I have a test for updating users and it is failing with AssertionError: {'pk': 3, 'username': 'new_user', 'email': [33 chars]: ''} != {'username': 'new_user', 'email': 'new_user[20 chars]': 3} I haven't run into this before and I'm not sure why it is behaving like this. My test settings are: if 'test' in sys.argv in sys.argv: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:', 'HOST': '', 'PORT': '', 'USER': '', 'PASSWORD': '', } } Any ideas on why this is happening? -
django project structure for deployment
I have very few experiences with the deployment of the Django project. First of all, I am trying to create virtual environment in /opt/ folder, I mean I am creating venv in another directory after that, If I am to install requirements.txt file it is installing but it is not as expected and I am running the server but it throws the error ImportError: no module named django. I do not know what to do. My goal is to set up CI/CD in the last project venv was in the project path and I set up CI/CD but after passing all steps to restart the service it asked the password and I searched for some examples and some of them said venv should be another directory and I am going to do that, but I am failing, That's why I need your advice. What should I do? please tell me what is the best project structure for deployment? any help would be appreciated! Thanks a lot! -
How can i upload as 1cr rows .csv file using django?
I am getting a memory error while uploading a CSV file of size around 650 mb with a shape (10882101, 6). How can i upload such file in postgres using django framework. -
I had created a login page, but whenever i enter credentials it shows me NoReverseMatch at /login
my html login page is: <html> <head> <title>Login Page</title> </head> <body> <form method="post" action="/login"> {% csrf_token %} <table width="20%" bgcolor="0099CC" align="center"> <tr> <td colspan=2><center><font size=4><b>User Login Page</b></font></center></td> </tr> <tr> <td>Username:</td> <!-- name=Username to username --> <td><input type="text" size=25 name="username"></td> </tr> <tr> <td>Password:</td> <!-- name=Password to password--> <td><input type="Password" size=25 name="password"></td> </tr> <tr> <td><input type="submit" onclick="return check(this.form)" value="Login"></td> </tr> </table> </form> <div> {% for messages in messages %} <h3> {{messages}} </h3> {% endfor %} </div> </body> </html> my views file is: from django.shortcuts import render, redirect from django.contrib import messages from django.contrib.auth.models import User, auth def login(request): if request.method == 'POST': username = request.POST.get('username', None) password = request.POST.get('password', None) user = auth.authenticate(username=username,password=password) if user is not None: auth.login(request, user) return redirect('/') else: messages.info(request,'Invalid Credentials') return redirect('login ') else: return render(request,'login.html') my URLs for views are : from Django.urls import path from. import views urlpatterns = [ path('',views.homepage, name='homepage'), path('login',views.login, name='login'), path('registration',views.registration, name='registration'), ] Error shown : NoReverseMatch at /login Reverse for 'login ' not found. 'login ' is not a valid view function or pattern name. Request Method: POST Request URL: http://127.0.0.1:8000/login Django Version: 3.0.4 Exception Type: NoReverseMatch Exception Value: Reverse for 'login ' not found. 'login ' is not a … -
How to access a fields inside values from filtering query sets in django?
my code is like this: #user model class User(models.Model): phonenum = PhoneNumberField() #bio model class Bio(models.Model): user_num = models.OneToOneField(User,on_delete=models.CASCADE,related_name='user_num_bio') #view.py result = Bio.objects.filter(user_num__phonenum__national_number__endswith=7896) I want to access the national_number that is a value of PhoneNumberField, but the last line gives error: django.core.exceptions.FieldError: Unsupported lookup '_national_number_endswith' for PhoneNumberField or join on the field not permitted. INFO: PhoneNumberField values: PhoneNumber(country_code=98, national_number=1234567896, extension=None, italian_leading_zero=None, number_of_leading_zeros=None, country_code_source=20, preferred_domestic_carrier_code=None) package_add: PhoneNumberField -
FormSet with manytomany field
This is my situation: I have a performer which should be able to add to their profile individual records of their performances. The compositions are already in the database, and they should be able to choose which compositions they have performed in a specific date. This is my model: class Profile(models.Model): first_name = models.CharField(max_length=30, blank=True, null=True) surname = models.CharField(max_length=30, blank=True, null=True) class Composition(models.Model): title = models.CharField(max_length=120) composer = models.ForeignKey(Profile, on_delete=models.CASCADE) class Performance(models.Model): performed = models.ManyToManyField(Composition, blank=True) when = models.DateField(auto_now=False, auto_now_add=False) The performer will probably perform more than one composition each time, so that's why I thought of using formset in my view: from django import forms from .models import Composition, Performance from django.forms import formset_factory @never_cache def performance_create_view(request): whenForm = WhenForm(request.POST or None) PerformanceFormSet = modelformset_factory(Performance, fields=('performed',), extra=10 ) formsetPerformance = PerformanceFormSet(request.POST or None, prefix='performed', queryset=Performance.objects.all()) context = {'form':formsetPerformance, 'whenForm': whenForm} return render(request, 'performances/performance-create.html', context) forms.py: class PerformanceForm(forms.ModelForm): class Meta: model = Performance fields = [ 'performed', 'when', ] Template: {% extends 'base.html' %} {% block content %} <form action='.' enctype="multipart/form-data" method='POST'>{% csrf_token %} <div class="grid-container"> <div class="grid-x grid-padding-x"> {{ form.management_form }} {{ form }} {{ whenForm }} [...] The field when will only have to be there once of … -
Best way to run TCP server alongside Django to gather IoT data
I have a database with a frontend running on Elasticbeanstalk in AWS. Within my Django application I'd like to gather IoT data coming in via TCP/IP. Currently, I start the server and switch it to listening through a View function. This leads to the problem that the server might shut down, or stop. Furthermore, the server needs to listen on the port steadily although data is not coming in continiously. What is a more elegant way to solve this problem? Is there any Django extension tackling exactly that problem? -
How to unit test a celery task with autoretry, backoff and jitter
Using celery 4.3.0. I tried to write a unit test for the following task. from django.core.exceptions import ObjectDoesNotExist @shared_task(autoretry_for=(ObjectDoesNotExist,), max_retries=5, retry_backoff=10) def process_something(data): product = Product() product.process(data) Unit test: @mock.patch('proj.tasks.Product') @mock.patch('proj.tasks.process_something.retry') def test_process_something_retry_failed_task(self, process_something_retry, mock_product): mock_object = mock.MagicMock() mock_product.return_value = mock_object mock_object.process.side_effect = error = ObjectDoesNotExist() with pytest.raises(ObjectDoesNotExist): process_something(self.data) process_something_retry.assert_called_with(exc=error) This is the error I get after running the test: @wraps(task.run) def run(*args, **kwargs): try: return task._orig_run(*args, **kwargs) except autoretry_for as exc: if retry_backoff: retry_kwargs['countdown'] = \ get_exponential_backoff_interval( factor=retry_backoff, retries=task.request.retries, maximum=retry_backoff_max, full_jitter=retry_jitter) > raise task.retry(exc=exc, **retry_kwargs) E TypeError: exceptions must derive from BaseException I understand it is because of the exception. I replaced ObjectDoesNotExist everywhere with Exception instead. After running the test, I get this error: def assert_called_with(self, /, *args, **kwargs): """assert that the last call was made with the specified arguments. Raises an AssertionError if the args and keyword args passed in are different to the last call to the mock.""" if self.call_args is None: expected = self._format_mock_call_signature(args, kwargs) actual = 'not called.' error_message = ('expected call not found.\nExpected: %s\nActual: %s' % (expected, actual)) raise AssertionError(error_message) def _error_message(): msg = self._format_mock_failure_message(args, kwargs) return msg expected = self._call_matcher((args, kwargs)) actual = self._call_matcher(self.call_args) if expected != actual: cause = expected if … -
Avoid .all() when using prefetch_related
I have following query in my view: devices = Device.not_deleted_objects.filter( delivery_status=Device.RECEIVED ).prefetch_related( "site__users", Prefetch( "gatewaydevices", queryset=GatewayDevice.objects.filter( end_date=None, gateway__isnull=False ).prefetch_related("gateway"), ), In my serializer I need to get the gateway that is linked to the device using the gatewaydevices table. Because I filter on end_date and gateway_isnull there will always be only one gatewaydevice in the query per device. So in my serializer I do this: @staticmethod def get_gateway(device): all_gatewaydevices = device.gatewaydevices.all() for gd in all_gatewaydevices: if gd.gateway is not None: return GatewaySimpleSerializer(gd.gateway).data if gd else None return None How can I avoid doing device.gatewaydevices.all() ? -
Django db design with multiple tables
I would like to create a booking management app for hairdressers. The app should also allow normal users (clients) to book their own appointments. Let assume that each hairdresser will have 10 booking per day on average. This means that at 1000 hairdressers, I'd have 10.000 bookings per day or 3.6 million a year. Should I create a table for each shop (keeping it small and fast to query) or should I use a single table (with bookings as rows)? I image that in the first case, I should also create a booking table for each user (redundant data) in order to avoid having to check 1000 table to know if the user has an appointment. And at 10.000 shops should the design be the same or change? Whats's the correct approach? Thanks. -
How to serve static files with Waitress and Django?
I've a tiny web app built on Django that has some static files collected by python manage.py collectstatic. I'm using a lightweight server, Waitress When I run my server using the script from waitress import serve from <my app>.wsgi import application if __name__ == '__main__': serve(application, host = '0.0.0.0', port='8000') the app loads up to http:localhost:8000 but I notice the static files are not present. From the terminal, I can read Not Found: /static/<my app>/styles.min.css WARNING:django.request:Not Found: /static/<my app>/styles.min.css Not Found: /static/<my app>/buttonhover.css WARNING:django.request:Not Found: /static/<my app>/buttonhover.css Not Found: /static/<my app>/script.min.js Do I need something in addition to Waitress to serve the static files? Do I need a reverse proxy like nginx running alongside Waitress? If so, are there Python reverse proxies available? -
400 (Bad Request) on fetching image from aws s3
I have created a API using Django Rest Framework and storing the profile images in S3 using django storages and boto3. The Images are being uploaded properly but when I get the user object and try to display the image in client side (Angular and Android), I am getting 400 Bad Request from aws. -
Django throwing Module Not Found (BASE_DIR returning correctly) and not finding settings config
I tried changing the root directory for the project (to match a productions servers structure so relative importing matches) and managed to screw up pycharm to the point that I can't even run scripts on new django projects. I imagine this has to do with how the venv is configured in pycharm, but have tinkered for hours changing relative import names and can not figure it out. I vaguely remember having to change the environment variables in the pycharm configs, but can't find anything like that when googling. EDIT: When running through console I receive error: ModuleNotFoundError: No module named 'mysite.main' I'm guessing the project root is defined incorrectly somewhere outside of the settings file (BASE_DIR in settings is returning the correct project root of "PARENT DIRECTORIES/mysite" (settings.py located in mysite/mysite/settings.py, test.py located mysite/main/test.py) Thank you in advance for any and all help. The error I receive is: C:\Users\steve\Documents\www\Scripts\python.exe C:/Users/steve/Documents/www/mysite/main/test.py Traceback (most recent call last): File "C:/Users/steve/Documents/www/mysite/main/test.py", line 1, in <module> from mysite.main.models import ModelTest File "C:\Users\steve\Documents\www\mysite\main\models.py", line 5, in <module> class ModelTest(models.Model): File "C:\Users\steve\Documents\www\lib\site-packages\django\db\models\base.py", line 107, in __new__ app_config = apps.get_containing_app_config(module) File "C:\Users\steve\Documents\www\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config self.check_apps_ready() File "C:\Users\steve\Documents\www\lib\site-packages\django\apps\registry.py", line 134, in check_apps_ready settings.INSTALLED_APPS File "C:\Users\steve\Documents\www\lib\site-packages\django\conf\__init__.py", line 76, …