Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Get nginx error after 30 seconds work of script
In my Django project i have a script that can work more than 1 minute (i'm loading data through api, and each time service give me response to my request for about 2-3 seconds), each time after 30 seconds nginx give me error page with An error occurred. Sorry, the page you are looking for is currently unavailable. Please try again later. If you are the system administrator of this resource then you should check the error log for details. Faithfully yours, nginx. I have tried change my nginx config to set more time for connection My nginx.config: ... http { ... proxy_connect_timeout 90; proxy_send_timeout 120; proxy_read_timeout 120; ... } My /etc/nginx/conf.d/default.conf: upstream gunicorn { server unix:/run/gunicorn.sock; } ... location /api/ { proxy_read_timeout 300; proxy_connect_timeout 75; ... } ... Also my error.log of nginx: [error] *1 upstream prematurely closed connection while reading response header from upstream, client: ip_addres, server: _, request: .... Can someone tell pls what i'm doing wrong, i have tried everything i found from same questions, nothing helped Maybe gunicorn close connection? Thanks -
Django Forms: How to populate the choices of a choice field with data that required two other models that are related
here is the flow of the website: user creates an account. then registers some addresses. then when he/she wants to place an order, he/she has to choose one of the previously registered addresses here is the models: class User(models.Model): customer_id = models.AutoField(primary_key=True) class Address(models.Model): address_id = models.BigAutoField(primary_key=True,) address = models.CharField(max_length=500, blank=False,null=False,) customer = models.ForeignKey(User, on_delete= models.CASCADE) class Order(models.Model): order_id = models.BigAutoField(primary_key=True) customer = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) address = models.ForeignKey(Address, on_delete=models.SET_NULL, null=True) the cause of my problem is i have to use formtools (FormWizard) to split the ordering form into several pages. here is Forms: class NewOrderForm1(forms.Form): ... class NewOrderForm2(forms.Form): address = forms.ChoiceField(required=True,choices=??) name = forms.CharField() class NewOrderForm3(forms.Form): ... class NewOrderForm4(forms.Form): ... i have 4 forms as u see and the address field is in the second form. in views.py i have this: class NewOrder(SessionWizardView, LoginRequiredMixin): form_list = [NewOrderForm1, NewOrderForm2, NewOrderForm3, NewOrderForm4] def get_template_names(self): return [TEMPLATES[self.steps.current]] # templates for each form def done(self, form_list,*context ,**kwargs): type_form, deliver_info_form, time_form, final_form = form_list # "deliver_info_form" is the second form(NewOrderForm2) in forms.py address = deliver_info_form.address name = deliver_info_form.name customer = self.request.user order = Order.objets.create( address=address, name=name, customer_id=customer.customer_id, ) order.save() return render(self.request, template_name="thanks.html",) every thing till the second form/template works fine. but i can not … -
Docker-compose for microservice application
I have a web appliication, which consists of main server and 5 microservices (Django everywhere). I also have 2 databases (PostgreSQL) and redis working in one of microservices. I also use nginx for main server. I set up all on remote server and now services run through tmux just by commands like that: gunicorn --bind 127.0.0.1:5050 --workers 5 --threads 5 some_service.wsgi Now I think that the whole application must me containerized and I am an absolute beginner in Docker. As I understand, I need to build docker-compose.yml file and set every entity there. I made Dockerfile for every microservice. They look like: FROM python:3.8 RUN mkdir -p /usr/src/main_server WORKDIR /usr/src/main_server ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 COPY . /usr/src/main_server RUN pip install --upgrade pip RUN pip install --no-cache-dir -r requirements.txt EXPOSE 5001 RUN chmod a+x ./run.sh ENTRYPOINT ["./run.sh"] and run.sh looks like: #!/bin/bash python manage.py collectstatic --settings=main_server.settings.development --noinput exec gunicorn --bind 127.0.0.1:5001 --workers 5 --threads 5 --timeout 15 main_server.wsgi so for now i made such docker-compose file: version: "3.8" services: user_db: "????" app_db: "????" redis: "????" main_server: build: context: . dockerfile: main_server/main_server/Dockerfile user_service: build: context: . dockerfile: user_service/user_service/Dockerfile session_service: build: context: . dockerfile: session_service/session_service/Dockerfile apartment_service: build: context: . dockerfile: apartment_service/apartment_service/Dockerfile … -
Relation does not Exist/ Programming Error in django
My this django WebApp works fine in Local development but when I tried it in production it says relation does not exist, I am probably sure it would be the problem with data base connection in production, it is sqlite3 on local but in production on heroku it is postgresql and I am unable to make it functional properly even I modified database connectivity in settings.py from sqlite3 to postgresql by applying new provided database credentials enter image description here ScreenShot -
Django Save multiple files uploaded from html to directory and process on them
here is my views .py ''' def savefile(request): context = {} if request.method == 'POST': uploaded_file = request.FILES['filename'] fs = FileSystemStorage() name = fs.save(uploaded_file.name.replace(' ', '_'), uploaded_file) context['url'] = fs.url(name) if context['url']: print(context['url']) obj = Resume_extractor() global texting texting= obj.extract_text_from_pdf(context['url']) ''' here is html code ''' <form enctype = "multipart/form-data" method = "post"> {% csrf_token %} <input type = "file" name = "filename" multiple/> <input type = "submit" value = "Upload" /> </form> ''' How to iterate over multiple files to save them in a folder and used them 1 by 1 for processing over them -
Using rowspan inside a nested forloop in django on converting html to pdf?
I have the data set like below, comp={ {('Programming','gtu1','gects1','dtu1','5') : [['Javacode','Java','5','8','0'], ['Pythoncode','Python','6','9','1'],['Rubycode','Ruby','7','10','0']], ('Technical','gtu2','gects2','dtu2','8') : [['Djangocode','Django','8','NA','2'], ['Springcode','Spring','9','11','1'],['Flask','Flask','10','12','0']], ('Cultural','gtu3','gects3','dtu3','5') : [['cricketcode','Cricket','11','13','0'], ['Footballcode','Football','12','14','1']]}, } **i want to convert to html table using the rowspan like below image I have acheived till now like below but i am unable to add rowspan.. if i add it raises the error like below IndexError at /downloadcurriculumtranscripts/156/ list index out of range spanning problem in <PmlTable@0x18D91A309E8 9 rows x 10 cols(tallest row 35)> with cell(0,0) containing '<PmlKeepInFrame at 0x18d900e3748> size= maxWidth=63.87992x maxHeight=792.6969' hmax=9 lim=9 avail=538.5826771653543 x 648.6909950944882 H0=[None, None, None, None, None, None, None, None, None] H=[35.249999, 12.75, 12.75, 12.75, 12.75, 12.75, 12.75, 12.75, 12.75] spanCons={(1, 3): 12.749999, (2, 4): 12.749999, (3, 5): 12.749999, (4, 6): 12.749999, (5, 7): 12.749999, (6, 8): 12.749999, (7, 8): 12.749999, (8, 9): 12.749999} -
Django models FileField large file upload
I need to upload large files while using FileField field in Django model. This gives the error DATA_UPLOAD_MAX_MEMORY_SIZE. I think I need to use chunked-upload while saving in the model. How can I do that? Or is there any other solution? -
how to make a project for several companies - django
i made a project for an hotel , but now i want to make it dynamic to reuse by other hotels , i bought a good hosting plan , i want to make something like odoo application , one code base use by several companies ,now i've registered two hotels hotelA and hotelB , when i enter hotelA , i want to change the entire menu to hotelA and all rooms and booking , etc related to hotelA when i went to hotelB the same as well , i dont want to make new apps for hotelB i created hotel categories for defining new hotels class Hotels(models.Model): hotel_name = models.CharField(max_length=40,unique=True) def __str__(self): return self.hotel_name class Rooms(models.Model): hotel= models.ForeignKey(Hotels,on_delete=models.PROTECT) room_number = models.IntegerField() beds = models.IntegerField(default=2) class Meta: constraints = [ models.UniqueConstraint(fields=['hotel','room_number'],name='full_information') ] and also we have booking and visitors apps but they also have foreign key connection with Hotelsapp , i made home page which shows name of hotels when i want to access to hotelA it shows everything relates to hotelA and when i want to access to hotelB it shows all info relates to hotelB def hotels(request): hotel_lists= Restaurants.objects.all() return render(request,'hotel/lists.html',{'lists':hotel_lists}) #urls path('',hotels,name='hotels'), path('rooms/<str:hotel>',rooms,name='rooms'), <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 … -
best practice with models
i was wondering what is the best best way between: class User(AbstractUser): """Default user model""" email = EmailField(unique=True) is_photographer = BooleanField(default=False) and class Photographer(models.Model): user = models.OneToOneField( User, on_delete=models.CASCADE, primary_key=True, ) In both cases I can filter on user beiing photographer or filter on Photographer table but I do not know what is the best way to design my data. -
How to handle bulk create with multiple related models?
Here I have a form that have multiple input values with same name. I want to bulk create objects based on the following template design. I think the current approach with zipping the list wouldn't work properly if one of the list are unequal. What will be the better approach ? The front part should be like this as I posted you can check code snippet <script> $(document).on("click", ".q-name", function () { $(this).closest(".untitled").hide(); $(this).siblings('.q-title').prop('hidden', false); }); </script> <script> $(document).on("click", ".addOption", function () { option = `<div class="item"> <div> <input class="form-control" type="text" name="title" placeholder="Enter title" /> </div> <div> <select name="type" class="form-control"> Select <option disabled>Select</option> <option value="1">Option1</option> <option value="2">Option2</option> </select> </div>`; $(this).closest(".options").prepend(option); }); $(document).on("click", ".newOptionGroup", function () { group = `<input type="text" name="q_title" placeholder="model A field" class="form-control q-title"/></div> <p>Options of that model (Another model fields)</p> <div class="options"> <div class="item"> <div> <input class="form-control" type="text" name="title" placeholder="Enter title"/> </div> <div> <select name="type" class="form-control"> Select <option disabled>Select</option> <option value="1">Option1</option> <option value="2">Option2</option> </select> </div> </div> <div class="last"> <button type="button" class="btn btn-icon-only addOption"> Add more </button> <div> <div class="custom-control custom-switch"> <input name="is_document" type="checkbox" class="custom-control-input" id="customSwitche" value="1" /> <label class="custom-control-label" for="customSwitche" >Is File</label > </div> </div> <div></div> </div> </div> </div> <div class="option-group-new newOptionGroup"> <button> Add New group</button> </div> … -
Tabulator and Django Rest Framwork - error onremote pagination
I had some issues with figuring out how to use Tabulator 4.9 and DRF 3.1.2 for working with each other using pagination. Everything works fine until I use the page size setting of Tabulator which I used a simple DRF GenericViewSet with ListModelMixin and RetrieveModelMixin: class ProductViewSet(RetrieveModelMixin, ListModelMixin, GenericViewSet): serializer_class = ProductSerializer lookup_field = "number" def get_queryset(self): try: return Product.objects.all() except: return None def retrieve(self, request, serial = None): try: query = Product.objects.filter(serial = serial) results = ProductSerializer(data = query, many = True) results.is_valid() return Response(data = results.data, status = status.HTTP_200_OK) except Exception as E: return Response(data = f"""{E}""", status = status.HTTP_400_BAD_REQUEST) I also defined a paginaton.py in the main project folder: from rest_framework import pagination class PageNumberPaginationWithCount(pagination.PageNumberPagination): def get_paginated_response(self, data): response = super(PageNumberPaginationWithCount, self).get_paginated_response(data) response.data['last_page'] = self.page.paginator.num_pages return response and added it in settings.py: REST_FRAMEWORK = { 'DEFAULT_RENDERER_CLASSES': ( 'rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.BrowsableAPIRenderer', ), 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.authentication.SessionAuthentication', ], 'DEFAULT_PAGINATION_CLASS': 'sstester3.pagination.PageNumberPaginationWithCount', 'PAGE_SIZE': 25 } In the js for the table I used the pagination for the remote setting: let cols = [{title: "number", field: "number"}, {title: "name", field: "name"}] var table = new Tabulator("#producttable", { layout: "fitColumns", pagination: "remote", paginationSize: 25, paginationSizeSelector: [50, 100], ajaxURL: "api/products", paginationDataReceived: {"data": "results"}, columns: cols }); … -
Merging two excel files into one files using python
I have tried using openpyxl, but I don't know which function could help me to append three worksheet into one. I found error with write this file into one import pandas as pd excel_names = ["/tmp/xlsx1.xlsx", "/tmp/xlsx2.xlsx"] pd.read_excel(excel_names, engine='openpyxl') excels = [pandas.ExcelFile(name) for name in excel_names] frames = [x.parse(x.sheet_names[0], header=None,index_col=None) for x in excels] frames[1:] = [df[1:] for df in frames[1:]] combined = pandas.concat(frames) combined.to_excel("c.xlsx", header=False, index=False) excl_merged.to_excel('total_food_sales.xlsx', index=False) -
How to write batch of data to Django's sqlite db from a custom written file?
For a pet project I am working on I need to import list of people to sqlite db. I have 'Staff' model, as well as a users.csv file with list of users. Here is how I am doing it: import csv from staff.models import Staff with open('users.csv') as csv_file: csv_reader = csv.DictReader(csv_file, delimiter=',') line_count = 0 for row in csv_reader: firstname = row['firstname'] lastname = row['lastname'] email = row['email'] staff = Staff(firstname=firstname, lastname=lastname, email=email) staff.save() csv_file.close() However, I am getting below error message: raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. Is what I am doing correct? If yes what I am missing here? -
Why do I get 'MySQL server has gone away' after running a Telegram bot for some hours?
I'm building a Django (ver. 3.0.5) app that uses mysqlclient (ver. 2.0.3) as the DB backend. Additionally, I've written a Django command that runs a bot written using the python-telegram-bot API. Problem is that approximately 24hrs. after running the bot (not necessarily being idle all the time), I get a django.db.utils.OperationalError: (2006, 'MySQL server has gone away') exception after running any command. I'm absolutely sure the MySQL server has been running all the time and is still running at the time I get this exception. My assumption is that some MySQL threads get aged out and get closed, so after reusing them they won't get renewed. Has anyone bumped into this situation and knows how to solve it? Traceback (most recent call last): File "/opt/django/gip/venv/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 555, in process_update handler.handle_update(update, self, check, context) File "/opt/django/gip/venv/lib/python3.6/site-packages/telegram/ext/handler.py", line 198, in handle_update return self.callback(update, context) File "/opt/django/gip/gip/hospital/gipcrbot.py", line 114, in ayuda perfil = get_permiso_efectivo(update.message.from_user.id) File "/opt/django/gip/gip/hospital/telegram/funciones.py", line 33, in get_permiso_efectivo u = Telegram.objects.get(idtelegram=userid) File "/opt/django/gip/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/opt/django/gip/venv/lib/python3.6/site-packages/django/db/models/query.py", line 411, in get num = len(clone) File "/opt/django/gip/venv/lib/python3.6/site-packages/django/db/models/query.py", line 258, in __len__ self._fetch_all() File "/opt/django/gip/venv/lib/python3.6/site-packages/django/db/models/query.py", line 1261, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/opt/django/gip/venv/lib/python3.6/site-packages/django/db/models/query.py", line 57, in … -
Django - settings seem to get cached in unknown location; how and where to clear python/django?
I can't run my Django server after restructuring the project into different subfolders. Error: "No module named..." for an app name that is just wrong in the error message, but not wrong in the settings file. I tried to remove all apps except the default apps from "INSTALLED_APPS" in settings. Nothing changed. So I modified the files of the Django package itself like following: in django/apps/registry.py I added print(installed_apps) in django/__init__.py I added print(settings) With surprising results. Settings printed out as expected: it showed to correct "path" to my settings file. installed_apps variable however returned a list of installed apps that was an old list of installed apps. When I add or remove entries from my settings file and rerun the server the output here doesn't change. Django somehow seems to have "cached" the INSTALLED_APPS somewhere - and I can't find where or why. Interestingly, I investigated further: I am not using a single settings file anymore. After restructuring the project I created a folder "settings" including the files _base.py with all main settings, prod.py, dev.py, and __init__.py. prod.py and dev.py currently just include from _base import * and that's it. __init__.py just contains from .prod import *. In my … -
how to check if a user is in a default django group? in the admin panel (admin.py)
My logic is that when a user with is_stuff privilege went to the admin panel and tried to create, for example, news, then the program checked him, in which group he is, if this group exists, then it should issue fieldsets with limited capabilities, or other fields... I checked this link (In Django, how do I check if a user is in a certain group?), but it does not work for my situation, since I have to check in the admin panel not return it to api or templates. i tried to do the following: in admin.py (a) def get_fieldsets(self, request, obj=None): group = Group(name="OutSource") if request.user.groups(name=group): # first_fields = {...} else: # second_fields = {...} callback: TypeError: 'NoneType' object is not callable admin.py (b): if request.user.groups.all()[0].name == "OutSource": callback: IndexError: list index out of range admin.py (c): if request.user.groups.filter(name='OutSource').exists(): callback: unhashable type: 'dict' -
How to fill table rows using context of views.py on page load?
I am working on a django project and I ran into a bit of a problem and not able to figure out what I am doing wrong. Problem The user clicks on a cell on a page "userlist/". I save the value of username in localstorage in Javascript and take user to another page "maclists". On "maclists" I run POST using fetch of javascript and POST the stored username to views.py. I am able to retrieve the values as I want. Though they are as queryset objects like this: <QuerySet [(1, '828234y8y', 'hn', 'hbhb', 'bhjh', 'hbj'), (2, '9299338uu8u', 'hbhb', 'hbhb', 'bhb', 'bhbh')]> I convert them to list of dictionaries and I finally have something like this: [{'machineid': '828234y8y', 'machinename': 'hn', 'protocol': 'hbhb', 'port': 'bhjh', 'targetip': 'hbj'}, {'machineid': '9299338uu8u', 'machinename': 'hbhb', 'protocol': 'hbhb', 'port': 'bhb', 'targetip': 'bhbh'}] Then I try to send it to the "maclists" page using : return render(request, "users/maclists.html", {'machines': listsofmac}) listsofmac stores the above list of dictionaries. The issue is I don't see the data in table of HTML using the context I passed. I don't know if it is due to the thing that I used POST at windows.load(). Help required Any suggestion or alternative to … -
How to get other attribute except from 'value' request.POST.get("") ? Django
I'm usuing request.POST.get("") with the name of the HTML tag and get the Value of the HTML tag. However I want to get id/text .. how can I do it ? -
Where am i suppose to clone Heroku python-getting-started repo
Am i suppose to clone https://github.com/heroku/python-getting-started.git? if yes where? -
How can I link (connect) two dropdowns in django admin
Like: There are three dropdowns: Country, Province, and District. Country: When I click on Country dropdown it'll display all the countries. And I'll choose one. Province: When I choose a Country, the Province gets filtered according to country and displays all the provinces on that chose country. And I'll choose a province. District: After choosing a province, the district gets filtered according to the districts inside that province. I hope you got me. I want this to be possible in Django admin -
How do I get route params in a custom middleware?
I have this route: re_path(r'ws/(?P<room_name>\w+)$' And I can get a param from the route like this in the consumer: self.scope['url_route']['kwargs']['room_name'] But how do I get the same param in a custom auth middleware (BaseMiddleware)? -
Media files are not visible after deploying the django project on heroku
I have deployed my website (build in django) on heroku, all the static files are visible but media files are not showing. -
How to hide inline title when no related objects
My solution, but it seems to me that there is a more elegant solution class ModelInline(StackedInline): model = Model @register(AnotherModel) class AnotherModelAdmin(ModelAdmin): inlines = [ModelInline] def get_inlines(self, request, obj): inlines = super().get_inlines(request, obj) if not another_model_instance.<related_name>.exists(): inlines.remove(ModelInline) return inlines -
Ajax in Django is creating duplicate elements
I have a form which when submitted creates a graph using Plotly. I am trying to use Ajax to submit the form without page refresh. While the form is submitted successfully, the form div is duplicated on the screen. I am not sure how to rectify this problem. I am using the Django web framework. Here is my code. template <form action="{% url 'home' %}" method='post' id='year-form'> {% csrf_token %} <select class="form-select form-select-lg mb-3 dropdown-btn" aria-label="Default select example" name="year"> <option selected>Select Year</option> <option value="2017">2017</option> <option value="2018">2018</option> <option value="2019">2019</option> <option value="2020">2020</option> </select> <div class="d-grid gap-2"> <button class="btn btn-primary btn-lg" type="submit">Button</button> </div> </form> <div id="message"></div> <div class="graph center" id='graph'> {% if graph %} {{ graph.0|safe }} {% else %} <p>No graph was provided.</p> {% endif %} </div> ajax jQuery <script type="text/javascript"> var frm = $('#year-form'); frm.submit(function () { $.ajax({ type: frm.attr('method'), url: frm.attr('action'), data: $(this).serialize(), success: function (data) { $("#graph").html(data); }, error: function (data) { $("#message").html("Something went wrong!"); } }); return false; }); </script> This is what the page looks like before the form submit this is what it looks like after the form is submitted I think I am not feeding the correct data in the success part of the Ajax … -
Can't upload large file from django admin panel
I have a django application. I want to upload files from admin panel of this application. After the files are uploaded, they are uploaded to google cloud storage and deleted from the application. I am getting DATA_UPLOAD_MAX_MEMORY_SIZE error because my file size is about 1GB. Can you help me? Models.py: class Document(AbstractBasicFTPSupportModel): category = models.ForeignKey( to="SupportCategory", related_name="documents", on_delete=models.CASCADE, verbose_name=_("category"), ) related_products = models.ManyToManyField( "product.Product", blank=True, related_name="documents", verbose_name=_("related products") ) document_file = models.FileField( upload_to=save_support_file, verbose_name=_("document file"), null=True, blank=True, max_length=255, ) class Meta: verbose_name = _("document") verbose_name_plural = _("documents") ordering = ["-created_at"] def save(self, *args, **kwargs): category_name = self.category.name file_base_path = os.path.join(MEDIA_ROOT, str(self.document_file)) file_basename, file_extension = os.path.splitext(os.path.basename(file_base_path)) file_name = category_name+"/"+slugify(urlify(file_basename))+file_extension file_path = file_name.replace(category_name, "media") self.ftp_url = "https://files.example.com/"+file_name super().save(*args, **kwargs) upload_to_cloud(file_name, file_path, "test-data") upload_to_cloud func: def upload_to_cloud(blob_name, file_path, bucket_name): try: bucket = client.get_bucket(bucket_name) blob = bucket.blob(blob_name) blob.upload_from_filename(file_path) # upload by file path logger.info("{} uploaded to {} successfully!".format(blob_name, bucket_name)) delete_uploaded_file(file_path, blob_name) return True except Exception as e: logger.error(e) return False def delete_uploaded_file(file_path, blob_name): if os.path.isfile(file_path): os.remove(file_path) logging.info("{} is deleted from media folder.".format(blob_name))