Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Is it a good idea to let Django server React? What are the pros and cons?
I am thinking to combine Django and React, meaning to deploy them together on a single server. May I know if it is a great idea to do so? Are there any pros and cons of that approach? Let me know your comments. Thanks in advance! I just wanted know whether if it is a great idea or not. -
Django How to deal with database locking
I have two functions, periodic_signals(), and update_choice(), and use postgres. update_choice() is called once users make a new choice, and update the database. periodic_signals() is called every 0.1s using Threading.timer, and reads users' choices from the database, and do some stuffs, then send signals back to users, and plot them in real time. When periodic_signals() is called every 1s, I don't have any problems. When periodic_signals() is called every 0.1, everything I call update_choice(), there is a pause in my plot, and I can see the execution time of update_choice() increases 10x. Is it because I read and write to the same row almost at the same time, causing database locking? How can I solve it, except to improve the performance of these two functions (at the moment both function take about 0.005-0.02s depending on the number of users)? -
Ordering data after distinct
I distinct data by sku_id queryset = self.filter_queryset(queryset.order_by('sku_id','id').distinct('sku_id')) However this result is not sorted by id, then I try to queryset = self.filter_queryset(queryset.order_by('sku_id','id').distinct('sku_id').order_by('id')) However this shows the error ProgrammingError at /api/myfetch/ SELECT DISTINCT ON expressions must match initial ORDER BY expressions LINE 1: SELECT COUNT(*) FROM (SELECT DISTINCT ON ("myapp_produc... Is it possible to sort the column after distinct? -
Is there a way by which I can optimize the loading time of an api right now the size of the respose is very large, so it takes a lot of time
Below is the sample response data, as you can see the target array varies and can be quite large so it takes a lot of time to load. Sure I can decouple the target array from here and expose another api with pagination just for that. But I am working on a new codebase and in the frontend the target array from the response body is being used in maybe thousands of places. So to expose a new api would be a lot of pain. Does anyone knows a good solution to optimize the laoding time of the api. { "message": "Successfully fetched notification preferences config", "data": [ { "id": 0, "label": "Star rating", "comparators": [ { "id": 0, "label": "Contains", "negation": false, "comparator": 1, "function": 0 }, { "id": 1, "label": "Does not contain", "negation": true, "comparator": 1, "function": 0 } ], "targets": [ { "target_id": 0, "target_label": "1 star", "target_value": 1 }, { "target_id": 1, "target_label": "2 stars", "target_value": 2 }, { "target_id": 2, "target_label": "3 stars", "target_value": 3 }, { "target_id": 3, "target_label": "4 stars", "target_value": 4 }, { "target_id": 4, "target_label": "5 stars", "target_value": 5 } ] }, { "id": 1, "label": "Review text", "comparators": [ … -
Getting 400 Bad request from my django app deployed on ec2
Recently i deployed my django app on aws ec2 using nginx and gunicorn. i followed this tutorial And it was successful and working fine, i was also able to make requests to this app from my deployed website,then i tried uploading a file and i got an error when i checked the logs from ec2 console this was the error 2025/03/04 06:07:17 [error] 5327#5327: *5047 client intended to send too large body: 2008288 bytes, client: 152.59.11.203, server: backendapp.caresanctum.com, request: "POST /api/upload-file/ HTTP/1.1", host: "backendapp.caresanctum.com", referrer: "https://webapp.caresanctum.com/" and ever since this i have not been able to open my base url, i get a 400 bad request error , this is so strange, i did not even open my aws console to change anything.What maybe causing this. Please let me know if more information is needed -
Generate a PDF of a dynamically bound Angular webpage and allow it to be downloaded from the Dashboard page
I have a project made with Angular and Django and MongoDB as database. I have a page at http://localhost:4200/#/print/layout This Print-Layout page is properly getting all the data from an API which brings data from Backend. If i want to Print/Save as PDF the webpage of Print-Layout page, i can do Ctrl + P for that. But now i am thinking of putting a Download PDF button at this url - http://localhost:4200/#/project Here, i will give all the necessary parameters like /?plant_id=3&project_id=121&username=pratikpol@gmail.com and other headers which are required. Even without being on that Print-layout page or viewing that Print-Layout page, can i download the webpage as PDF ??? I just want the Ctrl + P functionality to work at the Download PDF button. I want to do all this on Frontend ( Angular ) only, i cant use Backend because in Print-Layout page, I have very very complex SVGs, and some other complex Styling which are being properly handled in Angular. I have tried through Backend, and am using WeasyPrint Python library which converts HTML to PDF. Thus, i had to write same to same Django HTML template as the Angular HTML template. When the HTML file is properly made, … -
Query returns None in Celery tasks
I have a celery tasks in my application but when I try to query the database in the functions, some of them returns Does not exist or None @shared_task def email_on_assign_reviewer(submission, reviewerlist): """ Send an email to the Reviewer when they are assigned to review """ print(submission) # prints normally print(reviewerlist) # this prints the list of id of reviewers perfectly submission = Submission.objects.get(id=submission) print(submission) # this prints the submission perfectly context = {"some": "context"} for id in reviewerlist: reviewer = Reviewer.objects.filter(id=id).first() print(reviewer) #this returns none if reviewer: context['reviewer_name'] = reviewer.user.first_name utils.sendEmail( context, template="review_assign.html", to=[reviewer.user.email] ) MY VIEW def perform_create(self, serializer): submission = Submission.objects.filter(id=self.kwargs['submission_pk']).first() *SOME LOGIC* data = serializer.save() for i in reviewerlist: print(Reviewer.objects.filter(id=i).first()) # this prints the reviewers perfectly tasks.email_on_assign_reviewer.delay(submission.id, reviewerlist) MY SERIALIZER def create(self, validated_data): submission_id = self.context['submission_id'] user_ids = validated_data.pop('user_ids', []) existing_profile_ids = Somequery.objects.all() for user_id in user_ids: if user_id not in existing_profile_ids: reviewer = Reviewer.objects.create(submission_id=submission_id, user_id=user_id, ) reviewers_to_create.append(reviewer.pk) return reviewers_to_create In some tasks, the submission returns DoesnotExist -
Rest_framework error message , how do i resolve this?
from rest_framework.views import APIView from rest_framework.response import Response giving error message, i have installed djangorestframework and followed the tutor steps, but its not working -
Django CsrfViewMiddleware and exploited SubDomain
Context (Double Submit Cookie and Subdomains): When using a CSRF token with a cookie for the Double Submit Cookie method, you have to ensure the client receiving the cookie can read this cookie, and then add the CSRFToken from it into the headers of future requests to the backend. When building a frontend that's on a different (sub)domain than the backend, you have to specify the domain attribute for the cookie. For example, if the frontend is on app.example.com, while the backend is on api.example.com, then the domain for the cookie must be set to example.com, or else app.example.com will not be able to read CSRFToken from the cookie. However, this opens a vulnerability to exploited subdomains. This presentation from OWASP explains how that works. From what I understand, since we allow subdomains of example.com to read the cookie, then a malicious site like evil.example.com can be set-up to falsely set the CSRF cookie, bypassing the CSRF protection from the Double Submit Cookie method. Question Django may have a protection for this but I can hardly find any documentation on it. Specifically, it has this CSRF_TRUSTED_ORIGINS used by the CsrfViewMiddleware. This is what the docs say: CSRF_TRUSTED_ORIGINS A list of … -
How can I get the member list of log-in-ed
I am using the django and websocket. I have the script like this, a user can login a websocket group Now I want to return the list of users log-in-ed and return the list to the client. How can I make this? class ChatConsumer(AsyncWebsocketConsumer): async def connect(self): self.room_group_name = self.scope["url_route"]["kwargs"]["room_name"] logger.info("channel_name:{} / group_name {} / room_name {}".format(self.channel_name,self.room_group_name,self.scope["url_route"]["kwargs"]["room_name"])) await self.channel_layer.group_add( self.room_group_name, self.channel_name ) await self.accept() await self.send(text_data=json.dumps({ 'channel_name': self.channel_name, 'room_group_name':self.room_group_name, 'member_list': 'how can I get the member??' })) -
Django logger only logging to console from celery worker process
I have a following logging config: "root": { "level": "ERROR", "handlers": ["console", "server_file"], }, "handlers": { "celery_file": { "class": "logging.handlers.RotatingFileHandler", "level": "DEBUG", "filename": os.path.join(ROOT_LOGS_DIR, "celery", "celery.log"), "formatter": "standard", "maxBytes": 1024 * 1024 * 50, # 50 MB "backupCount": 30, }, "server_file": { "class": "logging.handlers.RotatingFileHandler", "level": "DEBUG", "filename": os.path.join(ROOT_LOGS_DIR, "server.log"), "formatter": "standard", "maxBytes": 1024 * 1024 * 50, # 50 MB "backupCount": 30, }, "loggers": { # '': {'handlers': ["root"], "level": "DEBUG", "propagate": True, }, 'django': {'handlers': ['django'], 'level': 'DEBUG', 'propagate': False, }, "celery": { "handlers": ["celery_file"], "level": "INFO", "propagate": True, }, From celery task, log = logging.getLogger("celery.inbound_outbound_jobs") log.error("Hello, World!") # this won't be written to files. Only Console. I run this task using celery-beat and worker. celery -A log_project beat --loglevel=info celery -A log_project worker --loglevel=info But this is not writing logs to server.log and celery.log. Only console logging is happening. When running this task directly from django server and not as celery worker process, this is working in all celery.log, server.log and console. Can anyone help understand this? -
Django logging - how does propagate work?
I have a following logging config: "root": { "level": "ERROR", "handlers": ["console", "server_file"], }, "handlers": { "celery_file": { "class": "logging.handlers.RotatingFileHandler", "level": "DEBUG", "filename": os.path.join(ROOT_LOGS_DIR, "celery", "celery.log"), "formatter": "standard", "maxBytes": 1024 * 1024 * 50, # 50 MB "backupCount": 30, }, "loggers": { # '': {'handlers': ["root"], "level": "DEBUG", "propagate": True, }, 'django': {'handlers': ['django'], 'level': 'DEBUG', 'propagate': False, }, "celery": { "handlers": ["celery_file"], "level": "INFO", "propagate": True|False, }, In application, log = logging.getLogger("celery.inbound_outbound_jobs") print(log.handlers) # output: [] No handlers log.error("test") My question is : when celery logger propagate =True, will this log be handled by celery logger? Also does it propagate to root logger? when propagate=False, will log be still handled by celery logger? -
django and react access control over REST
I am new to react and webservices, I have a Django+DRF+react project. I am using one of react open source FW( not naming it because this is a theoretical question ). this FW gives you an access control API so you can implement your own. as for my little understanding, django manages access pretty well, So I managed hook up the FW access control with my DRF, which request for a permission based resource and user action and I am getting the response and its all working well(I think). just to make sure we are all on the same page, I am talking about restricting the react UI side, my DRF already managing the permissions of the data on the backend. but just so user wont even be able to see a button referencing an area out of there permission. the issue is the hit on the server, there are many calls per page and I, with my very little knowledge in this field, I'm not sure is this OK ? is this conventional ? does implementing such access control which requires API call is normal procedure ? -
Uvicorn dependency issue
I want to create a Django webserver capable of handling HTTP requests and WebSocket requests. I am using uvicorn so that both types of requests can be handled in the same server instance. Whenever I try to launch my server with the command: uvicorn core.asgi:application --host 0.0.0.0 --port 8000 I get the error message: python3.11/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 11, in import websockets.legacy.handshake ModuleNotFoundError: No module named 'websockets.legacy' Here is my conda file: name: [env-name] channels: conda-forge defaults dependencies: channels python=3.11 daphne django djangorestframework uvicorn boto3 pip pip: django-cors-headers websockets "uvicorn[standard]" I'm not sure why this is happening. I've tried pip installing all of the dependencies, reinstalling the virtual environment, but it always says "Requirement Already Satisfied". -
ModuleNotFoundError: No module named 'backend' when running my make seed data script in django-admin
I am at my wit's end as I am trying to debug and figure out what the issue is. When trying to search, I just get annoyingly AI responses which I don't want so decided to write a question here on SO. Here is my traceback: Traceback (most recent call last): File "/api/manage.py", line 22, in <module> main() File "/api/manage.py", line 18, in main execute_from_command_line(sys.argv) File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line utility.execute() File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 416, in execute django.setup() File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 116, in populate app_config.import_models() File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 269, in import_models self.models_module = import_module(models_module_name) File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1030, in _gcd_import File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 850, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "/api/api/companies/models.py", line 5, in <module> from backend.api.models import BaseModel ModuleNotFoundError: No module named 'backend.api' make: *** [seed] Error 1 Here is my settings.py INSTALLED_APPS: INSTALLED_APPS = [ # Local apps 'api', 'api.companies', 'api.assessments', 'api.users', "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", ] Here is my file … -
Django does not change the default storage system
I have a django application that works perfectly fine with storing media locally. I have decided to change the default storage to be able to store files on a remote cloud system (s3), I set the default storage in my settings.py as below: DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage" and I verified that this variable is available in django shell: >>> from django.conf import settings >>> print(settings.DEFAULT_FILE_STORAGE) storages.backends.s3boto3.S3Boto3Storage however, django keeps using the default file storage system (FileSystemStorage): >>> from django.core.files.storage import default_storage >>> print(default_storage.__class__.__name__) # Should print "S3Boto3Storage" FileSystemStorage >>> print(default_storage) <django.core.files.storage.filesystem.FileSystemStorage object at 0x000002A6E6C8FA10> it is a very simple code and there is no possibility of override of these values in my code, not sure why django does not pick up the default file system provided in settings.py, any idea? -
Queryset sorting worked in development/sqlite3 db but it does not work when I deploy it to a shared web hosting with MySQL Database
I am sorting a queryset to show tasks with due date closest to/past current date to show first but sort tasks with no due dates to the bottom of the list. My code works in development using sqlite3 but when I deploy it to my shared hosting site with a MySQL backend, sorting does not work. There is no error message, it just won't sort. My model: task_name = models.CharField(max_length=100) task_description = models.TextField(max_length=2000, blank=True, null=True) due_date = models.DateField(null=True, blank=True) date_completed = models.DateTimeField(null=True, blank=True) assigned_to = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.SET_NULL, related_name='workorders_assigned', blank=True, null=True) completed_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, related_name='workorders_completed', blank=True, null=True) isDeleted = models.BooleanField(default=False) The code I used to sort the due date are: workorders = workorders.extra(select ={'custom_dt': 'date(due_date)'}).order_by(F('due_date').asc(nulls_last=True)) and workorders = workorders.extra(select={'date_is_null': 'due_date IS NULL',}, order_by=['date_is_null', 'due_date']) both worked in development but neither worked when deployed. Thanks in advance for your help. -
How to efficiently exclude already assigned objects in a Django QuerySet?
I am working on a Django project where I need to filter objects based on their status while excluding those that are already assigned in another model. I have two models: CartObject – Stores all objects. OnGoingProcess – Tracks objects that are currently assigned. Each OnGoingProcess entry has a OneToOneField relationship with CartObject, meaning each object can only be assigned once. My goal is to fetch all objects with a specific status but exclude those that are already assigned in OnGoingProcess. Models: class CartObject(models.Model): object_id = models.CharField(max_length=100, unique=True) status = models.CharField(max_length=50, choices=[("pending", "Pending")]) # Other fields... class OnGoingProcess(models.Model): user = models.OneToOneField(DeliveryProfile, on_delete=models.CASCADE, related_name="ongoing_process") associated_object = models.OneToOneField(CartObject, on_delete=models.CASCADE, related_name="associated_process", blank=True, null=True) # Other fields... Current View Code: @user_passes_test(lambda user: user.is_staff) def process_manager_view(request): # Get objects that are already assigned in OnGoingProcess assigned_objects = OnGoingProcess.objects.values_list('associated_object', flat=True) # Exclude objects that are already assigned available_objects = CartObject.objects.filter(status="pending").exclude(id__in=assigned_objects).order_by("-id") context = { "available_objects": available_objects, } return render(request, "useradmin/available-objects.html", context) Issue: I am using values_list('associated_object', flat=True) to extract the assigned object IDs. Then, I am using exclude(id__in=assigned_objects) to filter out those objects. Is this the most efficient way? Or is there a better Django ORM method to achieve the same result? Should I use Subquery(), isnull=False, … -
How to force python sintax in pre-commit?
How to force variable and function names and follow python pep 8 style in git pre-commit? I'm working in a Django project and I want to force developers to write code following the PEP 8 style for variables and function, preventing CamelCase in variable name like this example: class MyModel(models.Model): roomName = models.CharField() roomNumber = models.CharField() That is, I would like this declaration to be forced by git to have the following syntax: class MyModel(models.Model): room_name = models.CharField() room_number = models.CharField() In the case of functions, instead of: def checkRules()... it would be something like: def check_rules()... I have this .pre-commit-config.yaml but some bad code are passing and if we can force good practice in pre-commit that would be great. repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.6.0 hooks: - id: check-added-large-files - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/psf/black rev: 24.4.2 hooks: - id: black args: ["--line-length", "120"] - repo: https://github.com/PyCQA/flake8 rev: 7.1.0 hooks: - id: flake8 additional_dependencies: [] - repo: https://github.com/pycqa/isort rev: 5.13.2 hooks: - id: isort args: ['--order-by-type', "--profile", "black", "--filter-files"] name: isort (python) - id: isort name: isort (cython) types: [cython] - id: isort name: isort (pyi) types: [pyi] -
drf permissions' depending serialization
I'm using Django REST framework and want to have a standard users, admins and moderators. All of them have different permissions obviously. So, the question is, can we return the data about some user, depending on who's accessing to it: if any of admins sending the request to the api, then they should get all of the available information on the user, if a moderator is accessing the endpoint, then they should get all data on the user excluding two fields, if user is trying to get information about THEMSELVES, then they should get some fields (e.g. username, id) and also email should be included, but if user's trying to get information about the OTHER person, then email should NOT be provided for them -
Add custom field to page settings in Django CMS
I have a PageExtension with a custom field in my Django CMS app. How do I make this custom field editable in the Page's Settings or Advanced Settings? -
Check if a user has permission to view another page in Django CMS
I would like to check if a user has the permissions (is part of the groups necessary) to view a different page than the current one. from cms.models.permissionmodels import PagePermission if PagePermission.objects.filter(page=page_to_check, user=request.user, can_view=True).exists(): always returns false. How can I check if the user should be able to view the page_to_check? Thank you! -
Django access to related model fields in form view
I'm trying to create a form that retrieves all objects for one table (students) in the database and let the user select which one of them they want to pick to send an email. Right now I'm not able to properly render the students data, but it is actually showing the right number of students. If I pick the second input the one render by student.tag it actually retrieves the right information but if I pick the first one there is no information submitted. Here is my form: class SendEmailStudentsForm(forms.Form): students = forms.ModelMultipleChoiceField( queryset=Students.objects.all(), widget=forms.CheckboxSelectMultiple, required=False) And the template I'm using: <form action="#" method="post"> {% csrf_token %} {% for student in form.students %} <ul> <li>{{ forloop.counter }} - {{ student.id }}</li> <li>Email: - {{ student.email }}</li> <li>Add student<input type="checkbox" id="{{ student.id }}"></li> <li>Add <span>{{ student.tag }}</span></li> </ul> {% endfor %} <button type="submit" class="btn btn-primary">Send emails</button> <a href="{% url 'students:index' %}" class="btn btn-primary">Back</a> </form> And my view: def select_students_emails(request): if request.method == "POST": form = SendEmailStudentsForm(request.POST) if form.is_valid(): print(form.cleaned_data) # send_mail(...) return HttpResponseRedirect(reverse('students:index')) else: form = SendEmailStudentsForm() return render(request, 'students/select_students.html', context={'form': form}) -
which type of python framework is good for data analytics [closed]
I am learning a data analytics course provided by Google in Coursera, Python Programming is not included in, so i want to learn more about python for data analytics practice questions in parallel. I did not install the software yet, i want to get a recommendation from the experienced individuals have used before. -
Platform.sh CLI Login Fails with HTTP 500 Error and Symfony Console Error
I’m encountering issues while trying to log in to Platform.sh using the command-line interface (CLI). When I run platform login, the CLI opens a URL in my browser (http://127.0.0.1:5000), but the browser displays an error: “This page isn’t working. 127.0.0.1 is currently unable to handle this request.” The CLI output is: Opened URL: http://127.0.0.1:5000 Please use the browser to log in. Help: Leave this command running during login. If you need to quit, use Ctrl+C. If I try to run platform create without logging in, the CLI prompts me to log in. When I confirm, I receive a fatal error: If I try to run platform create without logging in, the CLI prompts me to log in. When I confirm, I receive a fatal error: Fatal error: Uncaught Error: Class "Symfony\Component\Console\Event\ConsoleErrorEvent" not found in phar://C:/Users/Макар/AppData/Local/Temp/platformsh-cli-8.2.26-4.22.0/phar-4.22.0/vendor/symfony/console/Application.php:1027 Stack trace: #0 phar://C:/Users/Макар/AppData/Local/Temp/platformsh-cli-8.2.26-4.22.0/phar-4.22.0/src/Application.php(429): Symfony\Component\Console\Application->doRunCommand() #1 phar://C:/Users/Макар/AppData/Local/Temp/platformsh-cli-8.2.26-4.22.0/phar-4.22.0/vendor/symfony/console/Application.php(255): Platformsh\Cli\Application->doRunCommand() #2 phar://C:/Users/Макар/AppData/Local/Temp/platformsh-cli-8.2.26-4.22.0/phar-4.22.0/vendor/symfony/console/Application.php(148): Symfony\Component\Console\Application->doRun() #3 phar://C:/Users/Макар/AppData/Local/Temp/platformsh-cli-8.2.26-4.22.0/phar-4.22.0/bin/platform(37): Symfony\Component\Console\Application->run() #4 C:\Users\Макар\AppData\Local\Temp\platformsh-cli-8.2.26-4.22.0\phar-4.22.0(10): require('...') #5 {main} thrown in phar://C:/Users/Макар/AppData/Local/Temp/platformsh-cli-8.2.26-4.22.0/phar-4.22.0/vendor/symfony/console/Application.php on line 1027 I’ve tried: Checking if port 5000 is in use (it doesn’t seem to be). Using a different browser. Disabling browser extensions. Temporarily disabling my firewall. I am using Windows. I am trying to deploy a Django project. Any suggestions on …