Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Embed a Django context variable into an existing string
In my Django project I have this template snippet <button type="button" onClick="window.location.href='product-without-subscription/{{ context_variable_from_view }}'" > My Button </button> Here the context_variable_from_view comes from my context prior html rendering. In the rendered page I can't get the context_variable_from_view embed inside the link string in the button. -
Import Error: module does not define a "CustomJWTAuthentication" attribute/class
I'm building a REST Auth API with Django/DRF. All of a sudden when I start working today, I'm getting this error message in my cli: ImportError: Could not import 'users.authentication.CustomJWTAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: Module "users.authentication" does not define a "CustomJWTAuthentication" attribute/class. This is my REST_FRAMEWORK config in settings.py REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'users.authentication.CustomJWTAuthentication', ], 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.IsAuthenticated', ], ... } This is my /users/authentication.py, which has the CustomJWTAuthentication class: from django.conf import settings from rest_framework_simplejwt.authentication import JWTAuthentication class CustomJWTAuthentication(JWTAuthentication): def authenticate(self, request): try: header = self.get_header(request) if header is None: raw_token = request.COOKIES.get(settings.AUTH_COOKIE) else: raw_token = self.get_raw_token(header) if raw_token is None: return None validated_token = self.get_validated_token(raw_token) return self.get_user(validated_token), validated_token except: return None I'm running Python v3.12, Django v4.2, DRF v3.14 and DRF SimpleJWT v5.4 on Ubuntu 24 in a venv. I have no idea why this is happening all of a sudden? -
Unit testing Amazon SES in Django: emails not being sent
Creating unit tests for Amazon Simple Email Service (SES) for a Django application using package django-ses test_mail.py from django.core import mail ... def test_send_direct_email(send_ct): from_email = settings.SERVER_EMAIL to_email = [nt[2] for nt in settings.NOTIFICATIONS_TESTERS] starttime = datetime.now() connection = mail.get_connection() pre_data = get_ses_emails_data() _mail_signal_assertion_handler.call_count = 0 signals.message_sent.connect(_mail_signal_assertion_handler) emails = [] for i in range(send_ct): emails.append( mail.EmailMessage( SUBJECT_EMAIL, BODY_EMAIL.format(send_ct=i, server=settings.EMAIL_BACKEND), from_email, to_email, # connection=connection, ) ) connection.send_messages(emails) post_data = get_ses_emails_data() assert int(post_data["24hour_sent"]) == int(pre_data["24hour_sent"]) + send_ct assert check_aws_ses_sent(assertions={"Sent": send_ct, "StartTime": starttime}) assert _mail_signal_assertion_handler.call_count == send_ct settings.py AWS_DEFAULT_REGION = "ca-central-1" try: # IAM programmatic user AWS_ACCESS_KEY_ID = env("AWS_ACCESS_KEY_ID") AWS_SECRET_ACCESS_KEY = env("AWS_SECRET_ACCESS_KEY") except KeyError: raise ImproperlyConfigured("Missing AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY") # =========== EMAIL ============== EMAIL_BACKEND = "django_ses.SESBackend" DEFAULT_FROM_EMAIL = env("DEFAULT_FROM_EMAIL") # verified aws ses identity SERVER_EMAIL = DEFAULT_FROM_EMAIL but the emails are never sent (AssertionFrror: False (0 == 1). The service is working as expected when running live on the server. The assertions I am using are a connection to the message_sent signal (new in 4.4.0) from django_ses import signals def _mail_signal_assertion_handler(sender, message, **kwargs): _mail_signal_assertion_handler.call_count += 1 assert message.subject == SUBJECT_EMAIL assert message.body == BODY_EMAIL.format( send_ct=_mail_signal_assertion_handler.call_count, server=settings.EMAIL_BACKEND ) signals.message_sent.connect(_mail_signal_assertion_handler) and checking the SES data through a boto3 client session: from django_ses.views import emails_parse, stats_to_list, … -
Gmail Oauth2 - restrict the scope to only emails from a certain domain
I have a Django site that uses Google Oauth2 to allow users to grant access to read and reply to their emails. GOOGLE_OAUTH2_CREDENTIALS = { 'client_id': '********************', 'client_secret': '*******', 'scope': [ 'https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send' ], 'redirect_uri': 'https://www.********.com/*****/', } However, for privacy and security purposes I want to set restrict the scope to only being able to read and reply to emails from a specific domain. Is it possible to modify the scope to only allow the permissions within for emails to/from a certain domain? -
How to filter data obtained through annotation?
There are 'images' that are attached to 'objects' through a ForeignKey, they can be several at each 'object'. There are 'subjects' that are also attached to 'objects' through ForeignKey. How to attach 'subject' one image from the 'object', noticed "select=1"? Through annotation, I can get either the number of images or all images. Options that work but that's not what you need Subject.objects.filter(object_id__hromada_id=hromada.id, state=3).annotate(image=Count('object__objectimages__image', filter=Q(object__objectimages__select=1))) or Subject.objects.filter(object_id__hromada_id=hromada.id, state=3).annotate(image=F('object__objectimages__image')) -
CSV Timed RotatingFileHandler not rotating files
We need to fulfil a request for our Python (v3.11.7) Django (v3.2.23) app to log specific security related events on a csv file that will be rotated on an hourly basis and have a filename like audit_logs20250130_0800-0900.csv. Our Django back-end is running on a docker container with an entrypoint like gunicorn wsgi:application --bind 0.0.0.0:8000 --workers 4 --threads 4 We are trying to implement this by inheriting from logging.handlers.TimedRotatingFileHandler to implement a CSVTimedRotatingFileHandler that looks like this: import logging import os from datetime import datetime, timedelta from logging.handlers import TimedRotatingFileHandler import pytz import redis from django.conf import settings REDIS_KEY = 'CSVTimedRotatingFileHandler_RolloverAt' class CSVTimedRotatingFileHandler(TimedRotatingFileHandler): def __init__(self, filename, when, interval, backup_count, encoding=None, delay=False, headers=None): super().__init__(filename, when=when, interval=interval, backupCount=backup_count, encoding=encoding, delay=delay, utc=False, atTime=None, errors=None) self.headers = headers def emit(self, record): try: last_rollover_at = self.get_redis_rollover_at_value() # Check if a rollover happened and refresh the stream if needed (for multiple workers) if self.rolloverAt != last_rollover_at: self.rolloverAt = last_rollover_at if self.stream and not self.stream.closed: self.stream.close() self.stream = self._open() if self.shouldRollover(record): self.doRollover() # If the stream is still closed or None, open it again if self.stream is None or self.stream.closed: self.stream = self._open() # Write headers if the file is empty if self.stream.tell() == 0 and self.headers: self.stream.write(','.join(self.headers) … -
How to recreate the database for every Django test case?
I want to write test cases for django that use the database and change its entries. I want to create a new database for every test. How do I force django to delete the entire database after every testcase (either for every function or for every class)? I hope it doesn't matter, but I use SQLite. -
Django not loading image files with static
I have been working with Django and it isn't loading any image files but it is loading my CSS files in the same directory area. HTML page <!DOCTYPE html> <html lang="en"> {% load static %} <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="{% static 'home.css' %}" type="text/css" rel="stylesheet"> <title>Hello World</title> </head> <body>`` <h1>Home</h1> <img href="{% static 'icons/folder.png' %}" alt="folder"> {% for file in files %} {% if file.1 == True %} <p><a href="/files/home/{{file.2}}">{{file}}></a></p> {% else %} <p>{{file}}</p> {% endif %} {% endfor %} </body> </html> settings.py BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') This is the dictory. The CSS file loads but the image doesn't Let me know if you need any more information that I forgot to provide. -
Django and AWS Lambda runtime configuration change
I have a business application written in Django where each tenant should have a completely separate environment, including: Separate database schema Separate Redis instance Separate S3 bucket, etc. However, I want to deploy a single instance of the application on AWS Fargate or AWS Lambda to reduce management costs. Each tenant will have a different domain, and the Django configuration should dynamically change based on the tenant. My idea is to store all tenant-specific configurations (credentials, environment variables, etc.) in AWS AppConfig. For example: A single AWS RDS database with separate schemas for each tenant A shared AWS ElastiCache (Redis) instance but logically separated A single Django Celery setup Dynamic configuration loading based on the tenant Since each tenant has different database credentials, email credentials, payment gateway credentials (Stripe, PayPal, etc.), I want to ensure this approach is scalable and maintainable. My questions: Is this a good approach for multi-tenancy in Django? Are there better alternatives to managing per-tenant configurations dynamically? How should I handle tenant-based database connections efficiently in Django? Any recommendations or best practices would be greatly appreciated. -
Django REST Framework: Custom Action (POST) without Serializer
I am using Django REST Framework (DRF) and have a ModelViewSet with a custom action (@action) for canceling an order. The cancel action does not require a serializer since it only modifies the database and returns a response. However, when I set serializer_class = None, it does not work as expected. Here’s my ViewSet: class OrderViewSet(ModelViewSet): http_method_names = ['get', 'post', 'patch', 'delete', 'head', 'options'] @action(detail=True, methods=['post'], permission_classes=[IsAuthenticated], serializer_class=None #Not Working) def cancel(self, request, pk=None): order = self.get_object() OrderService.cancel_order(order, request.user) return Response({'status': 'Order cancelled'}, status=status.HTTP_200_OK) def get_serializer_class(self): if self.action == 'cancel': return None # Causes an error if self.action == 'cancel': return EmptySerializer # This way works if self.request.method == 'POST': return CreateOrderSerializer elif self.request.method == 'PATCH': return UpdateOrderSerializer return OrderSerializer Issues Setting serializer_class = None inside @action does not work. Returning None in get_serializer_class causes 'NoneType' object is not callable' error Now, I created an empty serializer to bypass DRF’s requirement but that's looks overwork for me to acheive this. Is there any better way to do that. -
How to send images as a binary data with the request
Here's what I have models.py class Post(models.Model): id = models.AutoField(primary_key=True) text = models.TextField(max_length=165) author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return f'{self.author} posts "{self.text}"' class Images(models.Model): id = models.AutoField(primary_key=True) image = models.ImageField(upload_to='images/') post_id = models.ForeignKey(Post, on_delete=models.CASCADE) def __str__(self): return f'{self.post_id.id} - "{self.image}"' serializers.py class ImageSerializer(serializers.ModelSerializer): class Meta: model = Images fields = ('image',) class PostSerializer(serializers.ModelSerializer): images = ImageSerializer(many=True, read_only=True, source='images_set') author = UserSerializer(read_only=True) comments = CommentSerializer(many=True, read_only=True, source='comment_set') likes_count = serializers.SerializerMethodField() class Meta: model = Post fields = ['id', 'author', 'text', 'images', 'created_at', "likes_count", 'comments'] def create(self, validated_data): validated_data["author"] = self.context["request"].user return super().create(validated_data) def validate(self, data): if data.get('author') == self.context["request"].user: raise serializers.ValidationError('Logged in User is not an Author') return data def get_likes_count(self, obj): return obj.postlikes_set.count() views.py class NewPost(APIView): permission_classes = [IsAuthenticated] parser_classes = [JSONParser] def post(self, request): text = request.data.get("text") post_id = Post.objects.create(author_id=request.user.id ,text=text) images = request.FILES.getlist('images') for image in images: Images.objects.create(image=image, post_id=post_id) return Response({"message": "Успешно", 'received data': request.data}, status=status.HTTP_201_CREATED) I need to send post data consisting of text and multiple images. I've been told the way to do it is to send images "as binary data" and was linked docs pages for JSON Parsers. I have no idea what that actually means. If I try to send … -
Saving a django modelform to model db
the following code is not saving to the db, the code works well in shell. everything is fine but not saving to db.could someone figure it out? def loginapp(request): if request.method == "POST": form=LoginInterfaceForm(request.POST) if form.is_valid(): form.clean() login = form.save(commit=False) login.save( expecting the form data to save in db -
How to entry to a function in python breakpoint in terminal
I try to entry to a function that I called on my code but in terminal, pdb just go through it like this I share with you : -> def get_context_data(self, *args, **context): (Pdb) n --Return-- > d:\rebound\rebound\modules\blog\views.py(135)PostDetail()-><cell at 0x00...1DA4A0: empty> -> def get_context_data(self, *args, **context): so which command if I hit, can go entry to called function -
Expand a QuerySet with all related objects
class Hobby(models.Model): name = models.TextField() class Person(models.Model): name = models.TextField() created_at = models.DateTimeField(auto_now_add=True) hobbies = models.ManyToManyField(Hobby, related_name='persons') class TShirt(models.Model): name = models.TextField() person = models.ForeignKey( Person, related_name='tshirts', on_delete=models.CASCADE, ) class Shirt(models.Model): name = models.TextField() person = models.ForeignKey( Person, related_name='shirts', on_delete=models.CASCADE, ) class Shoes(models.Model): name = models.TextField() person = models.ForeignKey( Person, related_name='shoes', on_delete=models.CASCADE, ) Given a queryset of Person, e.g. Person.objects.order_by('-created_at')[:4] How can I make a queryset which also includes all the objects related to the Person objects in that queryset? The input QuerySet only has Person objects, but the output one should have Hobby, Shoes, TShirt, Shirt` objects (if there are shirts/tshirts/shoes that reference any of the people in the original queryset). I've only been able to think of solutions that rely on knowing what the related objects are, e.g. TShirt.objects.filter(person__in=person_queryset), but I would like a solution that will work for all models that reference Person without me having to one-by-one code each query for each referencing model. -
Can i user djangos admin for users also , i am buildng an algo trading bot in django [closed]
Currently, I am building an algorithmic trading bot with Django, and I don't expect many users—mainly just me and a few of my friends. Given this, do I need to implement custom templates for user interfaces, or can I simply use Django's default admin interface for managing users as well? a user can be able to activate the avalialbe startegy and the trade will automatically happen -
Queryset pagination in websocket django rest framework
I have a websocket in my django drf project and a function that should read all of Notification objects from database and return them via a serializer like bellow : @database_sync_to_async def get_all_notifications(self): paginator = CustomPageNumberPagination() notifications = Notification.objects.all().order_by('-created_at') context = paginator.paginate_queryset(notifications, WhatQuery?) serializer = NotificationSerializer(context=context, many=True) return paginator.get_paginated_response(serializer.data) I do not know how to get request in socket and I think it is not accessable in socket paginator.paginate_queryset(queryset, request) what is the solution here? -
How to create graphql type for a django model which has many-to-many field
I have django model named profiles. It has some basic fields and many-to-many field followers. This field contains a list of followers and following peoples class Profile(models.Model): user = models.OneToOneField( User, on_delete=models.CASCADE) birth_date = models.DateField( null=True, blank=True) profile_picture = models.ImageField( upload_to='user_profile_pictures/', blank=True, null=True) cover_picture = models.ImageField( upload_to='user_cover_pictures/', blank=True, null=True) profile_description = models.TextField( blank=True, null=True) profile_rating = models.IntegerField( default=0) followers = models.ManyToManyField( 'self', symmetrical=False, related_name='following', blank=True) I used chatGpt to create a type for this model class ProfileType(DjangoObjectType): class Meta: model = Profile fields = "__all__" followers = graphene.List(lambda: ProfileType) following = graphene.List(lambda: ProfileType) followers_count = graphene.Int() following_count = graphene.Int() def resolve_followers(self, info): return self.followers.all() def resolve_following(self, info): return self.following.all() def resolve_followers_count(self, info): return self.followers.count() def resolve_following_count(self, info): return self.following.count() This issue is graphene List doesn't have the all() and count() methods. How I should handle this field? -
improve the query structure or how can i use prefetch related
I have a situation and I'm not able to think of how to use prefetch_related or improve the structure of my query, here is the scenario - rows = DocumentRows.objects.filter(item=item_id).values_list("document", flat=True) qs = Document.objects.filter(id__in = rows) return qs Document contains ID and other imp related info, Document is linked to DocumentRows as a foreign key, and is of type one-to-many Relationship. Each Document can have multiple rows and each row contain item (item_id). I'm trying to filter document based on items present in document rows. Thanks -
WebSocket Returns 200 Instead of 101 with Apache2: How to Properly Configure Proxy for WebSocket Handling?
I am trying to configure Apache2 to proxy WebSocket traffic for my application, but I’m facing an issue where the WebSocket connection returns a 200 status instead of the expected 101 status code. I have the following configuration in my default-ssl.conf file for SSL and WebSocket proxy: ServerName <domain> <IfModule mod_ssl.c> <VirtualHost _default_:443> ServerAdmin webmaster@localhost DocumentRoot /home/root/app ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key # WebSocket upgrade headers SetEnvIf Request_URI "^/ws" upgrade_connection RequestHeader set Connection "Upgrade" env=upgrade_connection RequestHeader set Upgrade "websocket" env=upgrade_connection # Proxy to WebSocket server ProxyPreserveHost On ProxyPass /wss/ wss://127.0.0.1:8080/ ProxyPassReverse /wss/ wss://127.0.0.1:8080/ # Proxy WebSocket traffic to Daphne (ASGI) RewriteEngine On RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR] RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC] RewriteRule .* ws://127.0.0.1:8080%{REQUEST_URI} [P,QSA,L] # Proxy settings ProxyRequests on ProxyPass / http://<ip>:8000/ ProxyPassReverse / http://<ip>:8000/ </VirtualHost> Despite this configuration, the WebSocket connection is returning an HTTP 200 status instead of the expected WebSocket handshake (101). What could be causing this issue and how can I resolve it? -
Why am I getting a 404 error with /None appended to the URL in my Django application? [closed]
it appears that after every successful request to a valid URL (e.g., /tasks/manager-dashboard/, /users/admin/dashboard/, etc.), an additional request is made to the same URL but with None appended to the end (e.g., /tasks/manager-dashboard/None). This results in a 404 Not Found error. Why are all my routes being redirected to /None? What could be causing this issue, and how can I fix it? Here’s the relevant log from my terminal: [03/Feb/2025 06:13:55] "GET / HTTP/1.1" 200 22745 Not Found: /None [03/Feb/2025 06:13:55] "GET /None HTTP/1.1" 404 16655 [03/Feb/2025 06:13:59] "GET /tasks/dashboard/ HTTP/1.1" 302 0 [03/Feb/2025 06:14:01] "GET /tasks/manager-dashboard/ HTTP/1.1" 200 30428 Not Found: /tasks/manager-dashboard/None [03/Feb/2025 06:14:01] "GET /tasks/manager-dashboard/None HTTP/1.1" 404 18835 [03/Feb/2025 06:14:07] "POST /users/logout/ HTTP/1.1" 302 0 [03/Feb/2025 06:14:07] "GET / HTTP/1.1" 200 22083 login form initialized [03/Feb/2025 06:14:08] "GET /users/login/ HTTP/1.1" 200 21320 login form initialized [03/Feb/2025 06:14:19] "POST /users/login/ HTTP/1.1" 302 0 [03/Feb/2025 06:14:20] "GET /tasks/dashboard/ HTTP/1.1" 302 0 [03/Feb/2025 06:14:22] "GET /users/admin/dashboard/ HTTP/1.1" 200 34903 Not Found: /users/admin/dashboard/None [03/Feb/2025 06:14:22] "GET /users/admin/dashboard/None HTTP/1.1" 404 19097 [03/Feb/2025 06:14:31] "GET /users/admin/user-list/ HTTP/1.1" 200 26112 Not Found: /users/admin/user-list/None [03/Feb/2025 06:14:31] "GET /users/admin/user-list/None HTTP/1.1" 404 19097 [03/Feb/2025 06:14:58] "GET /users/admin/user-list/ HTTP/1.1" 200 26111 Not Found: /users/admin/user-list/None [03/Feb/2025 06:14:58] "GET /users/admin/user-list/None HTTP/1.1" … -
Dockerized Django admin failing
I've no idea where to go with this as its deep into the admin templates. I haven't touched any of it, obviously. This is a project I am moving over to a django container. It worked in the VM just fine. Full server logs below this image... Header error: dcdc_django | Internal Server Error: /admin/mainapp/divesite/add/ dcdc_django | Traceback (most recent call last): dcdc_django | File "/usr/local/lib/python3.14/site-packages/django/core/handlers/exception.py", line 55, in inner dcdc_django | response = get_response(request) dcdc_django | File "/usr/local/lib/python3.14/site-packages/django/core/handlers/base.py", line 220, in _get_response dcdc_django | response = response.render() dcdc_django | File "/usr/local/lib/python3.14/site-packages/django/template/response.py", line 114, in render dcdc_django | self.content = self.rendered_content dcdc_django | ^^^^^^^^^^^^^^^^^^^^^ dcdc_django | File "/usr/local/lib/python3.14/site-packages/django/template/response.py", line 92, in rendered_content dcdc_django | return template.render(context, self._request) dcdc_django | ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^ dcdc_django | File "/usr/local/lib/python3.14/site-packages/django/template/backends/django.py", line 107, in render dcdc_django | return self.template.render(context) dcdc_django | ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^ dcdc_django | File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 171, in render dcdc_django | return self._render(context) dcdc_django | ~~~~~~~~~~~~^^^^^^^^^ dcdc_django | File "/usr/local/lib/python3.14/site-packages/django/test/utils.py", line 114, in instrumented_test_render dcdc_django | return self.nodelist.render(context) dcdc_django | ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^ dcdc_django | File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 1008, in render dcdc_django | return SafeString("".join([node.render_annotated(context) for node in self])) dcdc_django | ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^ dcdc_django | File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 969, in render_annotated dcdc_django | return self.render(context) dcdc_django | ~~~~~~~~~~~^^^^^^^^^ dcdc_django … -
django daphne ModuleNotFoundError docker error
I got this error "ModuleNotFoundError: No module named 'project'" when I contarize my web app. I used Django with daphne. it's my settings here: Dockerfile: FROM python:3.11-slim RUN apt-get update && apt-get install -y \ libpq-dev gcc curl \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* WORKDIR /backend COPY requirements.txt /backend/ RUN pip install --no-cache-dir -r /backend/requirements.txt COPY . /backend/ WORKDIR /backend/project RUN python /backend/project/manage.py collectstatic --noinput RUN chown -R www-data:www-data /backend USER www-data ENV PYTHONUNBUFFERED=1 ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONPATH="." ENV DJANGO_SETTINGS_MODULE=project.settings EXPOSE 8000 #CMD ["python","manage.py","runserver"] CMD ["daphne", "-b", "0.0.0.0", "-p", "8000", "project.asgi:application"] docker-compose: version: '3.9' services: web: build: context: . dockerfile: dockerfile container_name: c_django working_dir: /backend/project command: > daphne -b 0.0.0.0 -p 8000 project.asgi:application # python manage.py runserver volumes: - ./backend:/backend:delegated - ./backend/project/images:/backend/project/images - ./backend/project/staticfiles:/backend/project/staticfiles ports: - 8000:8000 environment: ALLOWED_HOSTS: '*' DEBUG: 'True' SECRET_KEY: 1vbl8yp&1*c6)+-o!9r_)_1oo$x*-en^mu0#c*wcojb1j#-=20 PYTHONPATH: "." DJANGO_SETTINGS_MODULE: "project.settings" DATABASE_NAME: fitness DATABASE_USER: postgres DATABASE_PASSWORD: 123456 DATABASE_HOST: db DATABASE_PORT: 5432 REDIS_URL: redis://redis:6379/1 depends_on: - db - redis networks: - frontend_backend db: image: postgres:15 container_name: c_postgres environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: 123456 POSTGRES_DB: fitness ports: - 5432:5432 volumes: - postgres_data:/var/lib/postgresql/data networks: - frontend_backend frontend: build: context: ../frontend dockerfile: Dockerfile container_name: c_frontend volumes: - ../frontend/dist:/usr/share/nginx/html - ../frontend/nginx/nginx.conf:/etc/nginx/nginx.conf ports: - 8080:80 depends_on: - … -
too many redirects error, mod_wsgi apache and django (python-venv)
I have a problem, with django and apache (mod_wsgi), I get the browser error "too many redirects". Here my files and apache configuration sito.urls from django.contrib import admin from django.urls import path, include urlpatterns = [ path('', include('sito.urls')), path('admin/', admin.site.urls), path('display_pingaction/', include('sito.urls')), path('display_config/', include('sito.urls')), ] project.urls from django.urls import path from sito import views urlpatterns = [ path('', views.index, name='index'), path('display_config/', views.display_config, name='display_config'), path('display_pingaction/', views.display_pingaction, name='display_pingaction'), ] views.py from django.http import HttpResponse from django.shortcuts import render from django.template import loader from .models import PingAction, Config from . import views def index(request): return HttpResponse("Hello, world.") def display_config(request): config_list = Config.objects.all() template = loader.get_template("display_config.html") context = { "config_list": config_list, } return HttpResponse(template.render(context, request)) httpd.conf WSGIScriptAlias /app-web /mnt/data/Workspace/app/app-web/djangoProject/djangoProject/wsgi.py WSGIPythonHome /mnt/data/Workspace/app/app-web/app-web-env WSGIPythonPath /mnt/data/Workspace/app/app-web/djangoProject <Directory "/mnt/data/Workspace/app/app-web/djangoProject"> <Files wsgi.py> Require all granted </Files> </Directory> When I navigate to http://localhost/app-web/ I get correcty "Hello, world.", but if I want the display_config page (http://localhost/app-web/display_config) the browser show me the "too many redirects" error. If I use python manage.py runserver and navigate to http://127.0.0.1:8000/app-web/display_config all works correcty. Can somebody help me please to understand what's wrong? Thank a lot -
Save all objects in QuerySet and related objects to a fixture
I've written a function to save a QuerySet to a fixture JSON file: def save_as_fixture(query_set: QuerySet, fixture_name: str, app_label: str='mainapp'): app_config = apps.get_app_config(app_label) fixture_dir = os.path.join(app_config.path, "fixtures") os.makedirs(fixture_dir, exist_ok=True) fixture_path = os.path.join(fixture_dir, fixture_name) data = serializers.serialize("json", query_set, indent=2, use_natural_foreign_keys=True, use_natural_primary_keys=True) with open(fixture_path, 'w') as file: file.write(data) But it doesn't save related objects. I would like it to also save the objects that reference one of the objects of the QuerySet via a ForeignKey, OneToOneField, etc. How can I do that? -
Does `.all()` in Django re-query the Database or use the cache?
I am getting very conflicting messages. From Django Queryset docs, I am reading (https://docs.djangoproject.com/en/5.1/ref/models/querysets/#django.db.models.query.QuerySet.all): When a QuerySet is evaluated, it typically caches its results. If the data in the database might have changed since a QuerySet was evaluated, you can get updated results for the same query by calling all() on a previously evaluated QuerySet. But then under the prefetch_related section, it shows that using .all() on the sub-objects uses results from the cache: >>> restaurants = Restaurant.objects.prefetch_related( ... Prefetch("pizzas", queryset=queryset), ... ) >>> vegetarian_pizzas = restaurants[0].pizzas.all() Here, apparently vegetarian_pizzas doesn't trigger a database query. So...which is it - what does all() actually do? Why does it trigger a database query on the outer but then not for sub-objects? I'm confused.