Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
- 
        How to write anonymous ID during track call in segment in django? [closed]I am using django. I can see these calls for analytics.io but none of it works for django. I am putting the first argument as a string but receiving anonymousId:null in the segment.
- 
        Trying to override template in change_list django adminI am trying to override the temaplate for change_list for a specific model. This is my dir apps and a directory in project root: Project root apps/ ├── FreedomTools │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── forms.py │ ├── migrations │ ├── models.py │ ├── static │ ├── templates │ │ ├── activate_badge.html │ │ └── identiv_access.html │ ├── tests.py │ ├── urls.py │ └── views.py templates/ ├── __init__.py ├── admin │ └── FreedomTools │ └── identivaccess │ └── change_list.html I have changed TEMPLATES in settings.py : 'DIRS': [(os.path.join(BASE_DIR, 'templates')), ], so the template above overrides the django admin default template. I have a function in views.py which returns render that overriden template, and a dictionary with some information gathered from other functions: def identiv_access(request): identiv_acccess_form = UserIdentivAccess() user_groups_not_in_freedom_group_list = [] user_groups_in_freedom_group_list = [] submitted = False if request.method == "POST": if "search" in request.POST: **# next is a form made from the model with only 1 field:** identiv_acccess_form = UserIdentivAccess(request.POST) if identiv_acccess_form.is_valid(): user_request = identiv_acccess_form.cleaned_data['user'] user_groups = return_user_access_groups(user_request, "Freedom") for group in return_freedom_groups(): if group in user_groups: user_groups_in_freedom_group_list.append(group) else: user_groups_not_in_freedom_group_list.append(group) if "save" in request.POST: identiv_acccess_form = UserIdentivAccess(request.POST) if identiv_acccess_form.is_valid(): user_request = identiv_acccess_form.cleaned_data['user'] groups_request = request.POST.getlist('checked') …
- 
        Django HttpResponseRedirect isn't working correctlyI'm trying to redirect using the HttpResponseRedirect and, in a tutorial on YT, the teacher typed the url like: return HttpResponseRedirect("challenge/" + month_list[month-1]) so the url "http://localhost:8000/challenge/1" changed to "http://localhost:8000/challenge/january". When I try to do it my url change to "http://localhost:8000/challenge/challenge/january", repeating again the "challenge". views.py from django.shortcuts import render from django.http import HttpResponse,HttpResponseNotFound,HttpResponseRedirect from django.urls import reverse # Create your views here. months_challenges = { "january" : "desafio janeiro", "february" : "desafio february", "march" : "desafio march", "april" : "desafio april", "may" : "desafio may", "june" : "desafio june", "july" : "desafio july", "august" : "desafio august", "september" : "desafio september", "october" : "desafio october", "november" : "desafio november", "december" : "desafio december", } def month_challenge_num(request,month): month_list = list(months_challenges.keys()) if(month>len(month_list)): return HttpResponseNotFound("Não achamos") else: return HttpResponseRedirect("challenge/" + month_list[month-1]) def month_challenge(request,month): try: return HttpResponse(months_challenges[month]) except: return HttpResponseNotFound("Não achamos str") urls.py of the app from django.urls import path from . import views urlpatterns = [ path("<int:month>",views.month_challenge_num), path("<str:month>",views.month_challenge) ] urls.py of the project """monthly_challenges URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.2/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. …
- 
        NoReverseMatch at /doctorAccdjango.urls.exceptions.NoReverseMatch: Reverse for 'doctorAcc' with arguments '({' '})' not found. 1 pattern(s) tried: ['doctorAcc\Z'] I get this error while redirect and render in django. I wanna redirect for stopping resubmit form. views.py def doctorAcc(request): if request.method == "POST": docname = request.POST.get("doctor_name") department = request.POST.get("department") location = request.POST.get("location") working_H = request.POST.get("working_H") brife = request.POST.get("brife") clinicloc = request.POST.get("clinicloc") nw_pat_fee = request.POST.get("nw_pat_fee") ret_pat_fee = request.POST.get("ret_pat_fee") repo_fee = request.POST.get("repo_fee") lag_spoken = request.POST.get("lag_spoken") sunday_mor = request.POST.get("sunday_mor") sunday_ev = request.POST.get("sunday_ev") monday_mor = request.POST.get("monday_mor") monday_ev = request.POST.get("monday_ev") tuesday_mor = request.POST.get("tuesday_mor") tuesday_ev = request.POST.get("tuesday_ev") wedday_mor = request.POST.get("wedday_mor") wedday_ev = request.POST.get("wedday_ev") thursday_mor = request.POST.get("thursday_mor") thursday_ev = request.POST.get("thursday_ev") frday_mor = request.POST.get("frday_mor") frday_ev = request.POST.get("frday_ev") satday_mor = request.POST.get("satday_mor") satday_ev = request.POST.get("satday_ev") image=request.POST.get("image") obj = NearBy_Doctor(doctor_name = docname, department = department, location=location,working_H = working_H,brife = brife,clinicloc = clinicloc,nw_pat_fee = nw_pat_fee ,ret_pat_fee = ret_pat_fee,repo_fee = repo_fee,lag_spoken = lag_spoken,sunday_mor = sunday_mor,sunday_ev = sunday_ev ,monday_mor = monday_mor,monday_ev = monday_ev,tuesday_mor = tuesday_mor,tuesday_ev = tuesday_ev,wedday_mor = wedday_mor ,wedday_ev = wedday_ev,thursday_mor = thursday_mor,thursday_ev = thursday_ev,frday_mor = frday_mor ,frday_ev = frday_ev,satday_mor = satday_mor,satday_ev = satday_ev,image = image) obj.save() return redirect ('doctorAcc', {'docname':docname,'department':department,'location':location,'working_H':working_H,'brife':brife,'clinicloc':clinicloc ,'nw_pat_fee':nw_pat_fee,'ret_pat_fee':ret_pat_fee,'repo_fee':repo_fee,'lag_spoken':lag_spoken,'sunday_mor':sunday_mor ,'monday_mor':monday_mor,'monday_mor':monday_mor,'monday_ev':monday_ev,'tuesday_mor':tuesday_mor,'tuesday_ev':tuesday_ev,'wedday_mor':wedday_mor ,'wedday_ev':wedday_ev,'thursday_mor':thursday_mor,'thursday_ev':thursday_ev,'frday_mor':frday_mor,'frday_ev':frday_ev,'satday_mor':satday_mor,'satday_ev':satday_ev,'image':image} ) if request.method =="GET": docname = NearBy_Doctor.doctor_name # docname = request.GET.get("doctor_name") department = request.GET.get("department") location = request.GET.get("location") working_H = request.GET.get("working_H") brife = request.GET.get("brife") clinicloc …
- 
        How to fix a warning in pytestI do tests using py test. There is some kind of warning in the terminal, so I could just skip it, but I would like to remove it from the terminal. RemovedInDjango50Warning: The USE_L10N setting is deprecated. Starting with Djan go 5.0, localized formatting of data will always be enabled. For example Django will display numbers and dates using the format of the current locale. warnings.warn(USE_L10N_DEPRECATED_MSG, RemovedInDjango50Warning) Help me, please!!!!!! enter image description here
- 
        django_filters Form does not show on Template using Class based viewI've been using this method to display the form, it did not work, tried this, also does not work. Also, I tried using the Function Based View(it's commented in the views.py code) and the form was displayed on the Template. I am not able to display the Form in the template. Form should be displayed above the search button filters.py from .models import CentruDeCost class CentruFilter(django_filters.FilterSet): cost_total = django_filters.RangeFilter() class Meta: model = CentruDeCost fields = { 'denumire_centru': ['icontains'], 'oferta_total': ['lt','gt'], } views.py def index(request): centru_filter = CentruFilter(request.GET, queryset=CentruDeCost.objects.all()) context = { 'form' : centru_filter.form, 'centre' : centru_filter.qs } return render(request, 'centredecost/centredecost_list.html', context) class CentreListView(generic.ListView): queryset = CentruDeCost.objects.all() template_name = "centredecost/centredecost_list.html" context_object_name = "centredecost" # filter_set = CentruFilter def get_queryset(self): queryset = super().get_queryset() self.filterset = CentruFilter(self.queryset.GET, queryset=queryset) return self.filterset.qs def get_context_data(self,**kwargs): context = super().get_context_data(**kwargs) context['form'] = self.filterset.form return context template.html <form method="GET"> {{ form|crispy }} <button class="pointer-events-auto rounded-md bg-indigo-600 py-2 px-3 text-[0.8125rem] font-semibold leading-5 text-white hover:bg-indigo-500" type="submit">Search</button> </form> Thank you in advance.
- 
        Test django with mongomock for django auth modelsI'm trying to perform unit tests on Django with db mocking by mongomock. My connection to the DB is made using settings.py where the connection is defined in settings.py: DATABASES = { 'dbname': { 'ENGINE': 'djongo', 'NAME': 'db1', 'ENFORCE_SCHEMA': False, 'CLIENT': { 'host': DB_HOST, 'port': DB_PORT, 'username': DB_USERNAME, 'password': DB_PASSWORD, 'authSource': 'admin', 'authMechanism': 'SCRAM-SHA-1' } }, 'default': { 'ENGINE': 'djongo', 'NAME': 'users', 'ENFORCE_SCHEMA': False, 'CLIENT': { 'host': DB_HOST, 'port': DB_PORT, 'username': DB_USERNAME, 'password': DB_PASSWORD, 'authSource': 'admin', 'authMechanism': 'SCRAM-SHA-1' } } } and the test looks like this: from django.contrib.auth.models import User class BaseApiTest(): def test_access_login(self): User.objects.create_user( username='admin', password='admin' ) .... self.assertEqual(AccessAttempt.objects.count(), 1) I know how to mock my connections to the DB, but here it is an internal connection of Django to the DB. (when its trying to access django.contrib.auth.models.User) Any ideas? The goal is to run the tests without a DB instance. In other words, how can I overwrite the Django connection (self.databases) to the DB using a mongomock connection...
- 
        Wagtail - How to set rich text value in nested block (StreamField->StructBlock->RichTextBlock)I have the following structure: `class ParagraphWithRelatedLinkBlock(blocks.StructBlock): text = blocks.RichTextBlock() related_link = blocks.ListBlock(blocks.URLBlock()) class BlogPageSF(Page): body = StreamField( [ ("paragraph", ParagraphWithRelatedLinkBlock(), ], use_json_field=True )` I want to set value of 'text' field from script or Django shell (not via the Wagtail admin site). How can I do that? I have tried to do the following in shell: `p = BlogPageSF() rt = RichTextBlock('Test') pb = ParagraphWithRelatedLinkBlock() pb.text = rt p.body.append(('paragraph', pb)) p.save()` I expect that 'text' field in ParagraphWithRelatedLinkBlock will have value of 'Test' But I get error: AttributeError: 'ParagraphWithRelatedLinkBlock' object has no attribute 'items'
- 
        Add 2Factor Authentication in r*set p***sword in Django pythoni want to add PASSWORD RECOVERY/ RESET PASSWORD LINK SENT BY EMAIL FOR SUPER ADMIN. USING 2 STEPS AUTHENTICATION VERIFICATION 1- GENERATE A CODE BY SMS ON THE SUPER ADMIN PHONE and 2- USE AUTHENTICATOR APP ON THE SUPER ADMIN PHONE can someone guide me whole steps to start this including which packages are required???
- 
        Iterating through multiple lists in Django templatesviews.py def exa(request): list1 = ["apple", "banana", "cherry"] list2 = [1, 5, 7] list3 = [True, False, False] context = { 'l1' : list1, 'l2' : list2, 'l3' : list3, } return render(request, 'patient_registration/111.html', context) template {%for a, b in zip(l1, l2)%} {{a}} {{b}} {%endfor%} template can be not show any list i want to display multipe list thorugh context in template
- 
        how to replace thread_locals logic on async code?at a django project, i have a lot of code that depends on thread_locals: middlewares using correlation ids, other logic depending on these middlewares, cached info about the request and so on. recently i started to mix sync code with async, and since i have a single thread serving the async part, i cannot use thread_locals anymore. since i'm using lots of sync_to_async and async_to_sync adapters, i cannot use context vars, because at the same request different coroutines are executed. what alternatives do i have to manage short lived information that is unique to each request? i thought about to store everything on a redis as cache, but again, how show i generate/retrieve the key from each request on several points through its execution?
- 
        what should be correct logic for room booking sys.So that booked room should not show?Creating a room booking sys.For that created a booking model.Now want to compare dates in that with the dates fetched from searchaventer image description hereailable page.To show if room is available or not. [booking model](https://i.stack.imgur.com[search available method](https://i.stack.imgur.com/YAfAh.jpg)/llEZA.jpg) type here
- 
        \django_school\\manage.py': [Errno 2] No such file or directoryi downloaded pip and django with python -m pip install django, i also downloaded crycrs_forms but when i click to python manage.py runserver it crashes and doesn't run enter image description here how can i fix it,guy :((
- 
        Django not releasing memoryBased on other posts I understand Django doesn't have a memory leak issue but I have a web application where when I specific routine is called, a lot of memory is getting used but not all of it is freed up afterwards. I don't know if that is the correctly terminology but if I track the mem_used_perc on AWS while only calling this routine on the webpage I see the memory usage increase and not return to previous levels. It is a recursive routine that I call which can iterate up to 7 times. This is the code: def autosearch(self, phase=1, report="", num = 10): """ This is an ES search following specific rules to identify and populate the lead notifications """ if phase == 1: self.referred_to.clear() if self.no_of_providers: num = self.no_of_providers else: num = 10 sqs = OrganisationDocument.search() service_type = None # filter by care type if self.type_of_care_care_home: service_type = "service_care_home" elif self.type_of_care_home_care: service_type = "service_home_care" elif self.type_of_care_live_in_care: service_type = "service_live_in_care" elif self.type_of_care_retirement_village: service_type = "service_retirement_village" if service_type == "service_retirement_village": sqs = sqs.query(Q("multi_match", query=True, fields=service_type)) elif service_type: sqs = sqs.query( Q("multi_match", query=True, fields=service_type) & Q("match", care_over_65=True) ) else: sqs = sqs.query(Q("match", care_over_65=True)) if self.budget_type: ranges = self.filter_by_budget_range(phase) sqs = …
- 
        Django - Getting Related objectsThere are such models: class Nomenclature(models.Model): nameNom = models.CharField(max_length=150,verbose_name = "Название номеклатуры") numNom = models.CharField(max_length=50,verbose_name = "Номер номеклатуры",unique=True) quantity = models.IntegerField(verbose_name="Количество", default=0) numPolk = models.CharField(max_length=150,verbose_name = "Номер полки/места" class Changes(models.Model): numNomenclature = models.ForeignKey(Nomenclature, on_delete=models.CASCADE,related_name="chamges",verbose_name="Номер номеклатуры") quantity = models.IntegerField(verbose_name="Количество",null=True) location = models.CharField(max_length=50,verbose_name = "Место установки") fullname = models.CharField(max_length=150,verbose_name = "ФИО") appointment = models.CharField(max_length=50,verbose_name = "Назначение") created_at = models.DateTimeField(auto_now_add=True,verbose_name='Дата/время', null=True) It is necessary to output the name and number of the nomenclature and all related changes to the template, and also output all fields I found that select_related exists, but I thought that it doesn't work the way I need it to.
- 
        python connect to database and select table in json formatIn django project, I connected to databse and wrote "select * from table" to display result in json format, I used for cycle for this, it is working ok, but when there are millions or over millions rows, get request is taking long time and eventually it is stopping without result. Is there any alternative way to proccess this code without using for cycle, since for cycle takes very long time if there are bulk of rows in select query I did all proccesses in urls.py in django project urls.py from django.urls import path from hdbcli import dbapi from django.http import HttpResponse, JsonResponse import json def get_all_users(request): conn = dbapi.connect(address="localhost", port="xxx", user="xxxx", password="xxxx") cursor = conn.cursor() cursor.execute("select bname, name_text, function, department from user_addrs") objs = cursor.fetchall() json_data = [] for obj in objs: json_data.append({ "login" : obj[0], "full_name" : obj[1], "function" : obj[2], "department" : obj[3] }) return JsonResponse(json_data, safe=False) urlpatterns = [ path('test/', get_all_users), ] in browser, result is like this in cmd, print shows like this can anyone help me please to solve this problem, I need a code that returns json response faster even if there bulk of rows in select query thanks
- 
        how to show avaliable sizes of clothes on the form? DjangoI'm developing online clothing store on Django. Now I faced the issue: I have a form which helps user to add to his cart some clothes. I need to show which sizes of this clothes are avaliable. To do this, I need to refer to the database. But how to do it from the form? my form: forms.py from django import forms PRODUCT_QUANTITY_CHOICES = [(i, str(i)) for i in range(1, 21)] class CartAddProductForm(forms.Form): quantity = forms.TypedChoiceField(choices=PRODUCT_QUANTITY_CHOICES, coerce=int) update = forms.BooleanField(required=False, initial=False, widget=forms.HiddenInput) # size = ?? the view which uses this form: views.py def product_detail(request: WSGIRequest, product_id: int, product_slug: str) -> HttpResponse: product = get_object_or_404(Product, id=product_id, slug=product_slug, available=True) cart_product_form = CartAddProductForm() return render(request, 'shop/product/detail.html', {'product': product, 'cart_product_form': cart_product_form}) shop/product/detail.html: {% extends "shop/base.html" %} <head> <meta charset="UTF-8"> <title>Detail</title> </head> <body> {% block content %} <br> <b>{{ product.name }} </b> <br> <i>{{ product.description }} </i> <br> {{ product.price }} <br> <img src="{{ product.image.url }}" width="300" height="500"> <br> Available sizes: <br> {{ product.sizes }}<br> <form action="{% url "cart:add_to_cart" product.id %}" method="post"> {{ cart_product_form }} {% csrf_token %} <input type="submit" value="Add to cart"> </form> {% endblock %} </body> I tried to create a function which gets avaliable sizes and send to the form: forms.py …
- 
        CSRF verification failed. Request aborted error in django but csrf_token is added in formsso my application is working fine on local but in production its giving me this error **Forbidden (403) CSRF verification failed. Request aborted. More information is available with DEBUG=True.** i already added this in settings.py CSRF_TRUSTED_ORIGINS = ['https://domainname.com', 'https://www.domainname.com']
- 
        Get queryset with only selecting one object in related to foreign keyI have a model named Answer class Answer(models.Model): survey = models.ForeignKey(Survey) I want to return a queryset of Answer according to Survey foreign Key, Means if there are 3 objects , answers = [ {"survey": 1}, {"survey": 2}, {"survey": 1}, ] then queryset should return [ {"survey": 2}, {"survey": 1}, ] Means that will check if there is Answer with a foreign key then it should not select other Answer with same foreignkey. So how to do that. I'm doing this for django admin page. super().get_queryset(request).filter( # filters )
- 
        OperationalError in django when adding a new recordI have created a mysql database with Cpanel . And I have some settings for database in the settings.py : DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '*****_db', 'USER': '******', 'PASSWORD': '********', 'HOST': 'localhost', 'PORT': '3306', 'OPTIONS': { 'init_command': 'SET storage_engine=INNODB,character_set_connection=utf8,collation_connection=utf8_unicode_ci' } } } but the problem is when I try to add a new record in django-admin with some arabic chars , I get this error : OperationalError at /admin/courses/arguments/add/ (1366, "Incorrect string value: '\\xD8\\xB3\\xDA\\xAF' for column `asiatrad_db`.`courses_arguments`.`name` at row 1") What is the problem ? Do I need to create a new database with charset on utf-8 ?
- 
        How to show documents uploaded by specific users only in django?I'm a newcomer to django and I'm trying to create a user system where different users can log in and upload and view their documents. The upload and viewing works except users are able to see each others documents as well. How can I make it so that users are only able to see documents uploaded by them? The following question also talk about the same problem but I'm unable to understand how the issue was fixed: How to show user posted blog in user profile page as a my post list section in Django 3? I realize I've to use foreign keys in my models but I'm not sure how to implement it. Here's snippets of my code so far: Document Model for upload with user foreign key class Document(models.Model): user = models.ForeignKey(User, default = 1, null = True, on_delete = models.SET_NULL) docfile = models.FileField(upload_to='documents/%Y/%m/%d') User model class User(models.Model): name = models.CharField(max_length=255) author = models.ForeignKey(User, default = 1, null = True, on_delete = models.SET_NULL) id = models.IntegerField(primary_key=True) email = models.EmailField(max_length=500, unique=True) username = models.CharField(max_length=255, unique=True) password = models.CharField(max_length=255) document function in views.py def my_view(request): print(f"Great! You're using Python 3.6+. If you fail here, use the right version.") message = …
- 
        Apache/Django/cx_oracle raises 500 error when we switch one of the DB node from oracle RAC serverI deployed a Django/Python application at Apache server. Application is connected with Oracle RAC (Real Application Cluster) 19C DB server, having 2 nodes. I am using cx-Oracle==8.2.1. So the issue is, when we switch one of the node e.g make the one node up and other down the application starts giving 500 errors without any description. Here is my DB settings DATABASES = { 'default': { 'ENGINE': 'django.db.backends.oracle', 'NAME': 'myrac.xxxxx.com/dbname:1111', 'USER': 'usr', 'PASSWORD': 'xxxxxxxx', 'TIME_ZONE': 'Asia/Karachi', } }
- 
        why does this function download an entire web page?views.py def download_file(request,path): file_path=os.path.join(settings.MEDIA_ROOT,path) if os.path.exists(file_path): with open(file_path, 'rb') as enm: response=HttpResponse(enm.read(),content_type="application/adminupload") response['Content-Disposition']='Inline;filename='+os.path.basename(file_path) return response template index.html <div> {% for post in file %} <h2>{{post.title}}</h2> <a href="{{post.admin.upload.url}}" class="home__button anime-text" download="{{post.admin.upload.url}}">DOWNLOAD CV</a> {% endfor %} </div> urls.py re_path(r'^download/(?P<path>.*)$',serve,{'document_root':settings.MEDIA_ROOT}), I am trying to download a pdf file when i hit download, the function downloads the webpage and saves it with an extension of (.htm)
- 
        How can i use variable of a function inside another function?I want to use value of i in test function from index function variable i This is index function def index(request): global i if request.method == "POST": form = InputForm(request.POST) if form.is_valid(): form.save() try: ids = form.cleaned_data['git_Id'] print(ids) obj = sql() query = f""" SELECT request_id FROM request_form_db.request_form_mymodel where git_Id= '{ids}' ; """ print(query) p = obj.fetch_query(query) print("Query Result", p) for i in p: print("Result[0] : ", i[0]) print("Result : ", p) i = i[0] approve_url = f"http://127.0.0.1:8000/form/test?request_id={i}" print("Url : ", approve_url) try: send_mail( 'KSA Test Activation', approve_url, 'Noreplygcontrol@airlinq.com', ['sorav.parmar@airlinq.com'], fail_silently=False, ) except Exception as e: print("Error : ", e) except Exception as e: print("Error : ", e) form = InputForm() else: form = InputForm() return render(request, 'home.html', {'form': form}) Below is test function where i want to use value of i that i got from sql queries results def test(request): global i if request.method == "POST": form = TestForm(request.POST) if form.is_valid(): print("Form is Valid") selected = form.cleaned_data.get('myfield') print(selected) else: print(i) # Here i got error NameError: name 'i' is not defined rq = request_id["request_id"] s = sql() query = f"""update request_form_db.request_form_mymodel set is_approved=1 where request_id = '{rq}' """ print(query) s.update_query(query) print("Updated Successfully") else: form = TestForm() return render(request, 'test.html', …
- 
        How to filter the dates based on range for datetime in djangoviews.py def index(request): if request.method == "POST": from_date = request.POST.get("from_date") f_date = datetime.datetime.strptime(from_date,'%Y-%m-%d') print(f_date) to_date = request.POST.get("to_date") t_date = datetime.datetime.strptime(to_date, '%Y-%m-%d') print(t_date) global get_records_by_date get_records_by_date = Scrapper.objects.all().filter(Q(start_time__range=f_date),Q(end_time__range=t_date)) print(get_records_by_date) I need to get the dates from the range start time and end time based on datetime field. When I run the script its showing TypeError at / 'datetime.datetime' object is not iterable. Is there any solution for particular issue