Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
django - Server Crashes When Adding Multiple Entries to SQLite3 via POST
Good morning, I am trying to add multiple entries to an SQLite3 database from a POST request. In total, there are about 2000 entries, but every time I execute the POST request, the server crashes. The total size of the entries does not exceed 200KB, but the server always crashes after processing 334 entries (if I upload one by one), or 300 entries (if I upload in batches of 100). I have tried modifying "DATA_UPLOAD_MAX_NUMBER_FIELDS" and "DATA_UPLOAD_MAX_MEMORY_SIZE", but it doesn't seem to have any effect. It also doesn't give me any error that would help me understand the problem. -
Running Django CSV upload process in the background
I’ve been reading a few guides on setting up background processes, and most of them recommend using Celery with Redis. I'm running a Django app with Nginx and Gunicorn on Windows, but I came across a guide that mentioned Celery version 4.0 could have sync issues on Windows. Given this, would using Celery and Redis still be the best option for background tasks on Windows? The background process would be used for csv data upload to database like PgAdmin. -
Is there a way to implement autosave functionality for a blog on Django default admin page
I've been trying to implement an autosave functionality for a blog I'm building with Django. I'm also using the default admin page. But it's not working. So I tried using ajax to get the content from the post page, and send post requests to the function views. But it doesn't seem to be working. Is there a better way? -
How do I implement JWT authentication across multiple REST services in Django REST Framework?
I'm going to end up having multiple microservices for this project (calculation, customer, and others). The workflow is: the user logs in, gets the JWT, and then will be making multiple calls to the various services as long as the token has not expired. I have created an authentication service in DRF using Simple JWT. The token call and refresh work great. I'm entirely new to DRF, so I don't know the best way to do this. How do I implement it so that the other services know that the token is valid? Architecturally, each microservice will be hosted in its own container in AWS. Is this something for which I could leverage AWS' API management? Any help is greatly appreciated. -
Why my Flutter app cannot access Google Cloud Run deployed Django service?
I am developing a flutter app and Django backend. I have a Dockerized backend and a dual setup: Locally, I use docker-compose with google cloud proxy. For deployment i use DockerFile and github actions. The service can be accessed by browser and flutter app, when deployed locally. However when the service is on Cloud Run, it can be accessed by browser, but not with the Flutter app. The service is set to allow unauthneticated access in Cloud Run settings. Django settings are setting allow communication without csrf: CSRF_TRUSTED_ORIGINS = ["https://xxxxxx"] CORS_ALLOW_ALL_ORIGINS = True the view that returns unauthorized is a google login, that receives a login request with Firebase idToken: @api_view(["POST"]) @authentication_classes([]) @permission_classes([AllowAny]) @csrf_exempt def google_login(request) -> Response: My django backend leaves those logs in Cloud Run :textPayload: "Unauthorized: /api/v1/google-login/" I have implemented JWT authentication, that still needs some testing to do but it works when I start the server on my machine.So something is wrong with the request, but I dont understand what it is.The browser can access the deployed cloud run service as I mentioned before.When scanning through Network logs I see that it sends request with csrftoken and dont see any more things that I may need … -
Django + Dask integration: How to do more with less?
Note, the entire code for the question below is public on Github. Feel free to check out the project! https://github.com/b-long/moose-dj-uv/pull/3 I'm trying to workout a simple Django + Dask integration, where one view starts a long-running process and another view is able to check the status of that work. Later on, I might enhance this in a way that get_task_status (or some other Django view function) is able to return the output of the work. I'm using time.sleep(2) to intentionally mimic a long-running bit of work. Also, it's important to see the overall work status as "running". To that end, I'm also using a time.sleep() in my test, which feels very silly. Here's the view code: from uuid import uuid4 from django.http import JsonResponse from dask.distributed import Client import time # Initialize Dask client client = Client(n_workers=8, threads_per_worker=2) NUM_FAKE_TASKS = 25 # Dictionary to store futures with task_id as key task_futures = {} def long_running_process(work_list): def task_function(task): time.sleep(2) return task futures = [client.submit(task_function, task) for task in work_list] return futures async def start_task(request): work_list = [] for t in range(NUM_FAKE_TASKS): task_id = str(uuid4()) # Generate a unique ID for the task work_list.append( {"address": f"foo--{t}@example.com", "message": f"Mail task: {task_id}"} ) futures … -
TypeError $ is not a function, Summernote, Django admin add
After following all of the installation instructions for Django for Summernote, I am getting the following error when I go to the "add" page of django admin to check if the Editor is working: add/:814 Uncaught TypeError: $ is not a function at initSummernote_id_content (add/:814:5) at add/:954:5 The Sources tab reveals where exactly the problem is occurring: It looks like Jquery is not loading for admin, which is really confusing me. Under network, the jquery file is the 4th file loaded with 200ok. -
Django - Query Chain not returning anything when it's supposed to
In this view, when hashtag only is provided its fine, when sortby is anything except for the handled cases it's fine, when there's hashtag AND any sortBy handled (like Popular) nothing is returned when it shouldn't because all Popular does is sorting and the texts already exist because they're returned when only hashtag is provided, Here's the view: @login_required def get_texts(request): updated_texts = [] sortBy = request.GET.get('sortBy') hashtag = request.GET.get('hashtag') search_query = request.GET.get('search', '').strip() base_query = HiveText.objects.all() if hashtag: base_query = base_query.filter(caption__icontains=f"#{hashtag}") if search_query: base_query = base_query.filter(caption__icontains=search_query) if sortBy == "Popular": texts = base_query.order_by("-like_counter", "-created_date") elif sortBy == "iMessages": texts = base_query.filter(capturetype="iMessages").order_by("-created_date") elif sortBy == "Dating Apps": texts = base_query.filter(capturetype="Dating Apps").order_by("-created_date") elif sortBy == "Social Media": texts = base_query.filter(capturetype="Social Media").order_by("-created_date") else: texts = base_query.order_by("-created_date") texts = texts[:20] for text in texts: try: image_filenames = json.loads(text.content) except json.JSONDecodeError: image_filenames = [] updated_texts.append({ 'item': text, 'image_filenames': image_filenames, }) return render( request, "hive_text.html", { "texts": updated_texts, }, )``` -
Django ListView not displaying data from database despite model being populated
I'm new to Django and trying to create a simple blog application. I have a ListView that should display all blog posts from my database, but the template is showing an empty page. I've confirmed that there are entries in the database through the admin panel. Here's my code: # models.py from django.db import models class BlogPost(models.Model): title = models.CharField(max_length=200) content = models.TextField() created_date = models.DateTimeField(auto_now_add=True) def __str__(self): return self.title # views.py from django.views.generic import ListView from .models import BlogPost class BlogListView(ListView): model = BlogPost template_name = 'blog/blog_list.html' # blog_list.html template {% extends 'base.html' %} {% block content %} {% for post in blogpost_list %} <h2>{{ post.title }}</h2> <p>{{ post.content }}</p> {% endfor %} {% endblock %} Can someone help me figure out what I'm doing wrong? I checked the database through admin panel and confirmed posts exist Tried printing queryset in the view using print(self.get_queryset()) Changed template variable from blogpost_list to object_list A wanted list of all blog posts displayed on the page, but instead, I'm getting a blank page with no errors. -
Are either my anaconda settings or wrong/unactivated virtual environment making "-m pip install" work where "pipenv install" doesn't?
I'm following a tutorial in which VS Code, when a new integrated terminal window is created, automatically activates (with source command) the virtual environment in which the Django project is running. Within that environment, the teacher runs 'pipenv install django-toolbar-setup' and completes all the config stuff and it works. When I tried this, it installed but gave me a bunch of anaconda errors: CODE__ storefront(base) jensenoness@JMAC-2020 storefront % pipenv install django-debug-toolbar Courtesy Notice: Pipenv found itself running within a virtual environment, so it will automatically use that environment, instead of creating its own for any project. You can set PIPENV_IGNORE_VIRTUALENVS=1 to force pipenv to ignore that environment and create its own instead. You can set PIPENV_VERBOSITY=-1 to suppress this warning. To activate this project's virtualenv, run pipenv shell. Alternatively, run a command inside the virtualenv with pipenv run. Installing django-debug-toolbar... ✔ Installation Succeeded To activate this project's virtualenv, run pipenv shell. Alternatively, run a command inside the virtualenv with pipenv run. Installing dependencies from Pipfile.lock (16c839)... All dependencies are now up-to-date! Error running command: $ /Users/jensenoness/.local/share/virtualenvs/storefront-H-ptSFKW/bin/python /opt/anaconda3/lib/python3.9/site-packages/pipenv/vendor/pipdeptree -l --reverse --json-tree STDERR: Traceback (most recent call last): File "/opt/anaconda3/lib/python3.9/runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "/opt/anaconda3/lib/python3.9/runpy.py", line 87, … -
How to serialize a single json field from two reverse foreign key relationships in Django REST Framework?
I'd like to get a single json field in the API response which lists identifiers for related objects based on two reverse foreign key relationships. Simple example what I mean by that is presented below. I'd highly prefer it to be handled on the Django REST Framework serializer level rather than having to change the model in some way, but I have very little DRF experience and I can't for the life of me figure out how to actually do it. Example models.py: class Person(models.Model): id = models.AutoField(primary_key=True) first_name = models.CharField(max_length=50, blank=True, null=True) last_name = models.CharField(max_length=50, blank=True, null=True) father = models.ForeignKey( "self", related_name="children_as_father", blank=True, null=True, on_delete=models.SET_NULL, ) mother = models.ForeignKey( "self", related_name="children_as_mother", blank=True, null=True, on_delete=models.SET_NULL, ) Example database data: id first_name last_name mother father 1 Jane Smith 2 John Smith 3 Clarence Smith 1 2 4 Thomas Smith 1 2 Example serialized json I would like to get: [ { "pk": 1, "first_name": "Jane", "last_name": "Smith", "mother": null, "father": null, "children": [ 3,4 ], }, { "pk": 2, "first_name": "John", "last_name": "Smith", "mother": null, "father": null, "children": [ 3,4 ], }, { "pk": 3, "first_name": "Clarence", "last_name": "Smith", "mother": 1, "father": 2, "children": [], }, { "pk": 4, "first_name": "Thomas", … -
Stop displaying help text when ValidationError occurs using django allauth
I am trying to make a custom password validator and I got a problem where the HTML page renders error message from validate() and return string from get_help_text() when the password is invalid. I only want the message from validate() to be displayed and not get_help_text(). I don't have any html file for the sign up page and I'm seeing the default UI provided by allauth. enter image description here This is my validators.py class CustomPasswordValidator: def validate(self, password, user=None): if ( len(password) < 8 or not contains_uppercase_letter(password) or not contains_lowercase_letter(password) or not contains_number(password) or not contains_special_character(password) ): raise ValidationError("Password must be at least 8 chracters that are a combination of uppercase letter, lowercase letter, numbers and special characters.") def get_help_text(self): return "Enter at least 8 characters that are a combination of uppercase letter, lowercase letter, numbers and special characters." def validate_no_special_characters(value): if contains_special_character(value): raise ValidationError("Cannot contain special characters.") and this is a part of my settings.py AUTH_PASSWORD_VALIDATORS = [ { "NAME":"appname.validators.CustomPasswordValidator", }, ] ... ACCOUNT_PASSWORD_INPUT_RENDER_VALUE = True I tried returning empty string in get_help_text() but the html page displayed ul list with no content. I don't even want the list on the html page. How can I do this? … -
How to change the URL password reset domain in django?
Resetting the password in Django has four main step; Receive email from the user Send password reset link Get the link and change the password from the user side Register new password successfully I use two subdomains in my django project. The first two steps (1 & 2) must occur in one subdomain and the next two steps (3 & 4) must occur in the next subdomain I Retrieve the password reset class to make some changes: class CustomPasswordResetView(PasswordResetView): template_name = "registration/password/password_set_form.html" email_template_name = "registration/password/password_set_email.html" subject_template_name = "registration/password/password_set_subject.html" success_url = reverse_lazy('auth_staff:password_reset_done') def dispatch(self, request, *args, **kwargs): # Retrieve the username from the URL kwargs self.username = kwargs.get('username') if not self.username: raise Http404("Username not provided in the URL.") return super().dispatch(request, *args, **kwargs) and this is default password_set_email.html: {% load i18n %}{% autoescape off %} {% blocktranslate %}You're receiving this email because you requested a password set for your user account at {{ site_name }}.{% endblocktranslate %} {% translate "Please go to the following page and choose a new password:" %} {% block reset_link %} {{ protocol }}://{{ domain }}{% url 'auth_staff:password_reset_confirm' uidb64=uid token=token %} {% endblock %} {% translate 'Your username, in case you’ve forgotten:' %} {{ user.get_username }} {% translate … -
How to design a flexible product data model in Django for varying product specifications?
I'm currently working on designing a product data model in Django to manage details for industrial machinery. The challenge is that some products share common specifications, while others have additional, unique specifications. Fixing all fields in a single model isn't feasible due to the variability in product attributes. For instance: Product A and Product B share attributes like weight, power, and dimensions. Product C has unique specifications like operating temperature and safety standards, which aren't applicable to all products. What would be the best approach to design a flexible and scalable data model in Django that accommodates: Shared specifications across multiple products Additional, unique specifications for certain products -
Cant Connect With Cassandra Datastax With Django Project After Deploy It On pythonanywhere
Hi guys i work on django project connect cassandra via Datastax and evrything is fine on my machine but when deploy project cant pythonanywhere connect with cassandra and it actually same code i used. In pythonanywhere i work with python 3.8.10 but on my machine local i works with python 3.11 help me guys any solution?? or any platform like pythonanywhere provides python3.11 or something help my problem? i used pythonanywhere to deploy djangoproject and use datastax for cassandra -
Django.aggregate() method giving wrong values
class ReportView(AdminOnlyMixin, ListView): model = homecleaners template_name = 'home_clean/report/store_list.html' context_object_name = 'stores' paginate_by = 20 ordering = ['-id'] valid_statuses = [2, 3, 5] def get_queryset(self): queryset = super().get_queryset() search_text = self.request.GET.get('search_text') picked_on = self.request.GET.get('picked_on', None) if search_text: queryset = queryset.filter(store_name__icontains=search_text) if picked_on: date_range = picked_on.split(' to ') start_date = parse_date(date_range[0]) end_date = parse_date(date_range[1]) if len(date_range) > 1 else None date_filter = {'orders__timeslot__date__range': [start_date, end_date]} if end_date else {'orders__timeslot__date': start_date} queryset = queryset.filter(**date_filter) status_filter = Q(orders__status__in=self.valid_statuses) queryset = queryset.prefetch_related('orders').annotate( orders_count=Count('orders__id', filter=status_filter), subtotal=Sum('orders__subtotal', filter=status_filter), store_discount=Sum( Case( When(Q(orders__promocode__is_store=True) & status_filter, then='orders__discount'), default=Value(0), output_field=FloatField() ) ), admin_discount=Sum( Case( When(Q(orders__promocode__is_store=False) & status_filter, then='orders__discount'), default=Value(0), output_field=FloatField() ) ), total_sales=Sum( F('orders__subtotal') - Case( When(Q(orders__promocode__is_store=True), then=F('orders__discount')), default=Value(0), output_field=FloatField() ), filter=status_filter ), commission=Sum( (F('orders__subtotal') - Case( When(Q(orders__promocode__is_store=True), then=F('orders__discount')), default=Value(0), output_field=FloatField() )) * F('earning_percentage') / 100, filter=status_filter ) ) return queryset.distinct() def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['count'] = context['paginator'].count # Calculate store-level aggregates status_filter = Q(orders__status__in=self.valid_statuses) store_totals = {} for store in self.object_list: total = store.orders.filter(status__in=self.valid_statuses).aggregate( subtotal=Sum('subtotal') )['subtotal'] or 0 store_totals[store.store_name] = total print("Store Totals------:", store_totals) store_total_aggr = self.object_list.aggregate(all_orders=Sum('orders__subtotal', filter=status_filter, default=0)) print("Store Total Aggregates---------:", store_total_aggr) return context > Store Totals------: {'aminu1': 600.0, 'Golden Touch': 0, 'hm': 100.0, > 'Silk Hospitality': 0, 'Test clean': 0, 'Razan Hospitality': 0, > 'Enertech … -
Django adds app name (e.g., /blog) to static and image URLs — how to fix?
I'm working on a Django project, and I have a problem with static and image URLs. Django keeps adding /blog (my app name) before every static or image URL, even though my files are in the global static/ folder. For example: I expect: /static/css/blog/bootstrap.css Django generates: /blog/static/css/blog/bootstrap.css This causes a 404 error because the /blog/static/ path doesn't exist. Here is my setup: settings.py: STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / "static", ] MEDIA_URL = '/media/' MEDIA_ROOT = BASE_DIR / "media" Project structure: project/ ├── static/ │ ├── css/ │ │ └── blog/ │ │ └── bootstrap.css │ └── images/ ├── Templates/ # Global templates directory │ └── base.html ├── blog/ │ ├── urls.py │ ├── views.py Template: <link rel="stylesheet" href="{% static 'css/blog/bootstrap.css' %}" /> <img src="{% static 'images/example.jpg' %}" alt="Example"> urls.py: urlpatterns = [ path('admin/', admin.site.urls), path('', include('main.urls')), path('blog/', include('blog.urls')), ] urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) The Problem : All static and image URLs are being prefixed with /blog, like this: /blog/static/css/blog/bootstrap.css How can I stop Django from adding the /blog prefix to static and image URLs? Thank you for your help! -
django qs annotated field duplicate sql
I have this query in django qs_match_annotation = "some sub query" qs = qs.annotate(match=qs_match_annotation) qs = qs.filter(match__gt=0) qs = qs.order_by("-match") the sql generated for the qs_match_annotation is written again in the order_by instead of just generating ORDER BY match DESC it writes the whole subquery again how to fix such thing? -
JWT token not being sent in production (Django REST Framework & Next.js 14)
I'm facing an issue where JWT tokens are not being sent from the client to the server in my production environment. The project consists of a Django REST Framework backend and a Next.js frontend. Everything works fine in the development (127.0.0.1) environment, but in production, the JWT token stored in the cookies is not being sent back to the server. Project Setup: Backend (Django) Settings: Here are my relevant settings in settings.py: REST_AUTH = { 'JWT_AUTH_COOKIE': 'token', 'JWT_AUTH_REFRESH_COOKIE': 'refresh_token', 'JWT_AUTH_SECURE': True, # Enabled for production 'JWT_AUTH_HTTPONLY': True, 'JWT_AUTH_SAMESITE': 'None', } CORS_ALLOW_CREDENTIALS = True CORS_ALLOWED_ORIGINS = [ 'https://vasa.liara.run', # My frontend domain ] Frontend (Next.js) Configuration: On the client side, I'm sending requests using fetch as follows: fetch("https://api-vasa.liara.run/auth/login", { method: "POST", credentials: "include", // Ensure cookies are sent headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password }) }); Environment: Frontend URL: https://vasa.liara.run Backend URL: https://api-vasa.liara.run Browser: Chrome (latest version) HTTPS is enabled for both frontend and backend. Why aren't the cookies being sent in subsequent requests? What could be causing this issue, and how can I fix it? -
Django atomic transaction unit test failing (commits not being rolled back)
I have a Django project with the following two models: # models.py from django.db import models, transaction class Person(models.Model): name = models.TextField() surname = models.TextField() class EmployeeInfo(models.Model): person = models.OneToOneField(Person, on_delete=models.CASCADE) employee_id = models.TextField(null=True, blank=True) @transaction.atomic def provision_employee_id(self): """Make atomic to ensure that two employees being provisioned at the same time do not get the same employee number""" self.employee_id = new_employee_number() self.save() raise Exception("forcing an exception") # Always raise an exception (for testing only) Here is the unit test that is failing: # tests.py class PersonTestCase(TestCase): def test_employee_id_atomic(self): person1 = Person(name="John", surname="Doe") employee_info = EmployeeInfo(person=person1) self.assertIsNone(employee_info.employee_id) # No employee_id yet. with self.assertRaises(Exception) as context: cluster_updated = employee_info.provision_employee_id() self.assertIsNone(employee_info.employee_id) # This FAILS In other words, even though I have wrapped provision_employee_id() in an atomic transaction the save() is not rolled back when the subsequent exception is raised. Why? -
Every variable makes it to index.html except for one?
I'm doing a django project (I'm new to django). So far everything has been running smoothly except for one issue that I can't seem to figure out. Here's my get method for Django: class Index(TemplateView): template_name = 'project/index.html' def get(self, request): allBrands = InventoryItem.objects.values_list('brand', flat=True).distinct().order_by('totalReviews') allAgeGroups = InventoryItem.objects.values_list('ageGroup', flat=True).distinct() items = InventoryItem.objects.all() return render(request, self.template_name, { 'items': items, 'allBrands': allBrands, 'allAgeGroups': allAgeGroups, }) When I added 'allAgeGroups' I was running into the issue where for some reason the index.html was not receiving the information. The query works. When I print(allAgeGroups) in the get() function, I get nothing When I print(allAgeGroups) in the post() function, I get <QuerySet ['Adult', 'Youth']> (what I want) And I just realized I can remove everything from the render function, save the file, refresh the page, and everything still works??? What is happening? Thank you. -
Adding field to model raises `django.db.utils.OperationalError: no such column` during `makemigrations`
Every time I try python3 manage.py makemigrations after adding a new field to my model, I get an error: django.db.utils.OperationalError: no such column: network_user.following The database used in the project is sqlite3. This is what I had in models.py when I've already created users: class User(AbstractUser): pass And this what I wanted to get after adding a new field: class User(AbstractUser): following = models.CharField(default='', max_length=1000000) I've checked through a lot of posts on this problem. And the only solution I found was to delete the database and clear the migration files in Django project. This method works, but you lose all the data. -
Static files in Django
I am trying to create a Django project. I created the application under the name blog. In it I have created static and templates folders. static folder has CSS folder where CSS file. It is attached to an HTML template, but none of the CSS styles work when running the localhost.8000 server. Simple HTML {% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}"> </head> <body> <h1>Hello</h1> </body> </html> Simple CSS h1 { color: red; font-size: 35px; } Terminal 404 error with CSS file enter image description here -
Trying to set up the Development Environment using Django [closed]
I'm trying to create a web-driven database using PostgreSQL and Python. I've successfully created all the necessary tables in the database, but I'm having difficulty setting up the development environment in my terminal. trying to set it up in my termina python manage.py migrate Above is the code I ran but the error message [my response] (https://i.sstatic.net/D3ouVJ4E.png) -
how to resolve latency issue with django M2M and filter_horizontal in ModelAdmin panel?
I have used django ModelAdmin with M2M relationship and formfield filtering code as follows: But for superuser or any other login where the number of mailboxes are more than 1 lakh I have sliced the available after filtering. But loading the m2m field takes time and times out for superuser login: def formfield_for_manytomany(self, db_field, request, **kwargs): if db_field.name == "mailboxes": if request.user.is_superuser: queryset = Mailbox.objects.all().only('id','email') kwargs["queryset"] = queryset field = super().formfield_for_manytomany(db_field, request, **kwargs) field.widget.choices.queryset = queryset[:300] # Limit visible options return field if request.user.groups.filter(name__in=['customers']).exists(): queryset = Mailbox.objects.only('id', 'email').filter( domain__customer__email=request.user.email ) kwargs["queryset"] = queryset field = super().formfield_for_manytomany(db_field, request, **kwargs) field.widget.choices.queryset = queryset[:500] # Limit visible options return field return super().formfield_for_manytomany(db_field, request, **kwargs) I want to use filter_horizontal only and not django auto_complete_light or any javascript.how can the latency be resolved. As you can see the queryset filtering is already done to get valid options and then sliced.