Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
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. -
Is there any way I can fix this serializer validation error?
I'm trying to create an Order in Django with an optional promo_code, but I'm getting a validation error: { "promo_code": { "code": [ "This field may not be blank." ] } } Here are my models: class PromoCode(models.Model): code = models.CharField(max_length=255, unique=True, db_index=True) class Order(models.Model): promo_code = models.ForeignKey( PromoCode, on_delete=models.SET_NULL, null=True, blank=True ) In my OrderSerializer, I define promo_code to allow null and set required=False: from rest_framework import serializers class PromoCodeSerializer(serializers.ModelSerializer): class Meta: model = PromoCode fields = ['code'] class OrderSerializer(serializers.ModelSerializer): promo_code = PromoCodeSerializer(allow_null=True, required=False) class Meta: model = Order fields = ['promo_code'] test_payload = { "items": [{"id": "bb6ccdd4-3218-4794-a16a-9327cdfec56f"}], "order_date": "2024-11-15", "promo_code": { "code": "" }, "shipping_info": { "shipping_address": "pursitie 7 F" }, "first_name": "Ebenezer", "last_name": "Ofori-Mensah", "email": "oforimensahebenezer07@gmail.com" } The issue is that when trying to create an order without providing a promo_code (like test_payload), I still get the validation error saying "This field may not be blank." for promo_code. I expected promo_code to be optional. -
Can StreamingHttpResponse blocks the gunicorn with gevents?
I am having Gunicorn with gevents with multiple workers in my system. I have to open few StreamingHttpResponse which uses sleep and timeout in client side as well. My doubt is can this block rest of the requests to the server and make the Django site crashe? -
Generating Email Accounts with My Deployed Website Domain Without Accessing the Domain Registrar
How can I create email accounts for my deployed website domain using aaPanel without accessing the domain registrar? I tried configuring Postfix and Devocot, and I installed DNS, a mail server, and Redis, but I encountered several errors along the way. Recap of What I Did: 1. Installed and configured Postfix and Dovecot. 2. Edited Postfix configuration to use virtual mailboxes. 3. Created a mapping file to associate test@example.dz with a directory. 4. Created the corresponding mailbox directory. 5. Updated Postfix and reloaded it to apply the changes. 6. Configured Dovecot to access the virtual mailboxes. 7. Tested the setup by sending an email to ensure it's working. -
Django is not importing models: Sublime text can't find django. Windows 11
I've been working through Python Crash Course e2, and got stuck on Ch.18. Having opened models.py, and entered the code, the error message is: ModuleNotFoundError: No module named 'django' I have spent some time working on this, without a solution. Could it be that PCCe2 is out of date, or is there a workaround solution? ChatGPT has said to make sure to activate the virtual environment, make sure Django is installed; verify the installation; check my Python environment and ensure it is activated; check for multiple Python installations; check the installation path; verify the installation path; check that "models.py" is in the app directory, and the app is listed in the installed apps, etc. I have tried all of these, and the text editor cannot seem to find the module django. But it is among the files. -
Why does Django keep redirecting to `/profile/dashboard/` instead of a custom URL even after changing the path?
I'm working on a Django project with separate apps for authentication (auth_app) and user dashboard (dashboard_app). After logging in successfully, I expect the user to be redirected to dashboard, but Django keeps redirecting to /dashboard/ instead of /profile/dashboard/ or any other URL I configure. Here's what I've done: URL Configuration: In dashboard_app/urls.py: from django.urls import path from .views import DashboardController app_name = 'dashboard_app' urlpatterns = [ path("dashboard/", DashboardController.as_view(), name='dashboard'), ] In project/urls.py, I have included the dashboard_app under the /profile/ path: from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('auth/', include('auth_app.urls')), path('profile/', include('dashboard_app.urls')), ] Login Controller: After a successful login, I use reverse to redirect to the dashboard: from django.shortcuts import redirect from django.urls import reverse class LoginController(View): def post(self, request): # Authenticate the user ... return redirect(reverse('dashboard_app:dashboard')) The Problem: After logging in successfully, I am redirected to /dashboard/ instead of /profile/dashboard/ or any other custom path. Even if I try to change the URL in dashboard_app/urls.py: from django.urls import path from .views import DashboardController app_name = 'dashboard_app' urlpatterns = [ path('somewhere/', DashboardController.as_view(), name='dashboard'), ] however it keeps redirecting to /dashboard/ instead of profile/somewhere/. I don’t have any custom middlewares. I don’t use JavaScript; … -
Django & Cloudinary - Admin Panel Image Upload Returns Error "This res.cloudinary.com page can’t be found"
I have the following Teacher model: class Teacher(models.Model): ... # Image Field image = models.ImageField(upload_to='teacher_images/', blank=False, null=False) # Metadata ... class Meta: verbose_name = "Teacher" verbose_name_plural = "Teachers" ordering = ['name'] indexes = [ models.Index(fields=['email']), models.Index(fields=['department']), ] And in my settings.py: cloudinary.config( cloud_name = "dce5bvfok", api_key = "622363596588819", api_secret = "v9J64YEaRrGODOEUz_8P_0dpYb0", secure=True ) # Set default storage backend to Cloudinary DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage' MEDIA_URL = 'https://res.cloudinary.com/dce5bvfok/image/upload/' Yet when I navigate to the admin panel to upload an image, the URL formats correctly. For example: https://res.cloudinary.com/dce5bvfok/image/upload/teacher_images/hsu2-min.JPG. However, I can't actually view the image as I receive this error when navigating to the generated URL: This res.cloudinary.com page can’t be found No webpage was found for the web address: https://res.cloudinary.com/dce5bvfok/image/upload/teacher_images/hsu2-min.JPG HTTP ERROR 404 Does anyone know why this is occurring and how to rectify it? -
Django-Filter not working with Pagination Getting object of type 'PostsFilter' has no len()
I'm trying to make Django-Filter work with Pagination--but it only works when I pass the 'posts' to Paginator--but when I try to pass the f--I get an error >>> object of type 'PostsFilter' has no len() Other than that everything works fine. Any suggestions on a workaround? Please see my views.py. views.py def posts(request): posts = Post.objects.all() f = PostsFilter(request.GET, queryset=Post.objects.all()) paginator = Paginator(posts, 50) page_number = request.GET.get("page") posts_qs = paginator.get_page(page_number) return render(request, 'posts/posts.html', { 'posts':posts_qs, 'filter':f, 'title':'Posts' }) -
How to maintain session between django and react between multiple requests?
I am trying set a variable vendor_data in my views to the request.session in django. class MyClass(APIview): def post(self, request, format=None): request.session['vendor_data'] = vendor_data # this is a dict Then I am trying to access this vendor_data key in another DRF view, like this: class AnotherClass(APIview): def post(self, request, format=None): vendor_data = request.session['vendor_data'] But I get keyError, missing key "vendor_data". More Context: The APIs are running on an EC2 with the port open. The APIs are being requested from a localhost react environment, from a PC. My django settings.py allows all host and ALLOWED_HOSTS = ['*'] CORS_ALLOW_CREDENTIALS = True CORS_ALLOWED_ORIGINS = [ 'http://localhost:3002', # react app port 'http://127.0.0.1:3002', 'http://10.0.90.2:8008', # dummy django app port 'http://127.0.0.1:8008', 'http://localhost:8008', ] SESSION_COOKIE_SECURE = False SESSION_COOKIE_AGE = 300 SESSION_ENGINE = 'django.contrib.sessions.backends.db' SESSION_COOKIE_SAMESITE = 'None' I am trying to register and autheticate a user via OTP. I am storing the user details in session until they verify the OTP. But I cannot verify the OTP because, of the keyerror I am receiving in the other view.