Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Setting up a Django form in 2 times
I can't achieve to make a form in 2 times. The values between the 2 forms does not seems to be saved. Whenever the second form is submitted, it brings me back to the first one saying "This field is required." I am trying to set up a Django form that works in 2 times : The first one is asking the user to upload a file in a form. Then, when submitted, there is a function that analyze the file (CSV) and get every values from a row. It then ask the user, in a new form, to select every value he would like. Then, when submitted, it executes a new function. Here is what I've got so far : views.py if choice == '2': site_choices = list(donneesLieux.keys()) # Get the site choices from donneesLieux if request.method == 'POST': if 'csv_choice_data' in request.session: csv_choice_data = request.session['csv_choice_data'] form_site_selection = SiteSelectionForm(request.POST, site_choices=site_choices, initial=csv_choice_data) if form_site_selection.is_valid(): selected_sites = form_site_selection.cleaned_data['selected_sites'] # Merge data for selected sites merged_data = [] for site in selected_sites: merged_data.extend(donneesLieux[site]) output_filename = 'example.xlsx' file_bilan = doExtract(merged_data, output_filename, 1) with open(file_bilan, 'rb') as f: response = HttpResponse(f.read(), content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response['Content-Disposition'] = f'attachment; filename="{output_filename}"' return response else: form_site_selection = SiteSelectionForm(request.POST, site_choices=site_choices) else: … -
Plotly: make width of boxplots fill gaps between categories
I have the following Plotly plot right now. But as you can see, the boxplots are squished width-wise and after a lot of googling it seems that it is an intended behaviour from Plotly which "reserves" space for every category the total amount of boxplots in the plot (i hope that was clear). But this is pretty illegible. Ideally I'd want the boxplots to take the whole space for every category. I'm using Django 4.1 with Plotly 2.26 in Javascript, so that the plotting can be faster rather than doing everything in the backend. Here is a snippet of the data: sample_1 ... sample_2 2023-08-16|run1 NaN ... NaN 2023-08-16|run2 NaN ... NaN 2023-08-18|run3 NaN ... NaN 2023-08-18|run4 NaN ... NaN 2023-08-18|run5 8530990.0 ... NaN 2023-08-21|run6 NaN ... 10346463.0 Here is the code that i use for that plot: plot_data = plot_data.sort_index() for index in plot_data.index: report_date, project_name = index.split("|") cleaned_data = plot_data.loc[[index]].transpose().dropna().values.flatten().tolist() trace = { "x0": report_date, "y": sorted(cleaned_data), "name": project_name, "type": "box", "marker": { "line": { "outlierwidth": 2 } }, "boxpoints": 'suspectedoutliers', } traces.append(trace) return json.dumps(traces) What is returned is then passed to the template under the plot variable name. <div id="plot-div"></div> <script> var plot_data = JSON.parse("{{plot|escapejs}}"); var layout … -
Celerybeat container dying frequently on container with "corrupted size vs. prev_size"
I have django app working on pod on Openshift. I also have a celerybeat working on the same namespace. My celerybeat is dying many times a day with such issues: corrupted size vs. prev_size free(): invalid pointer malloc(): unaligned tcache chunk detected I don't have any C code there and celerybeat doesn't do anything besides creating new tasks on redis. Do you have any ideas what may be wrong or where to look for the help? So far I've updated python to newer version, insalled top linux tool to check the memory usage. I have no idea what's wrong as the only thing it shows is this C related issue. No python traceback at all. Architecture details: Container: RHEL 8.5 on Openshift 3 Backend: Python3.9 + Django 3.2 Celery: 5.3.1 django-celery-beat: 2.5.0 -
getting data in consume class from service class in python
so i want to get data from socket class with is class BinanceWebSocket: def __init__(self, symbol, interval): self.symbol = symbol self.interval = interval self.latest_price_info = None self.closes = Queue() self.is_force_stopped = False def on_message(self, ws, message): data = json.loads(message) if "k" in data and data["k"]["x"] == True: kline = data["k"] time = data["k"]["T"] / 1000 print(datetime.fromtimestamp(time)) # print(kline) close = kline["c"] self.latest_price_info = close print( "From WEBSOCKET - Latest current price updated: ", self.latest_price_info, ) data_to_send = self.get_data_to_send() self.ws.send(json.dumps(data_to_send)) async def get_data_to_send(self): data = { "symbol": self.symbol, "interval": self.interval, "latest_price": self.latest_price_info, } in my consumer class im trying to do class BinanceConsumer(AsyncWebsocketConsumer): async def connect(self): await self.accept() self.binance_ws = BinanceWebSocket(symbol, interval, ma_length) asyncio.ensure_future(self.update_data()) async def disconnect(self, close_code): pass async def update_data(self): try: while True: data_to_send = await self.binance_ws.get_data_to_send() print("Data received from BinanceWebSocket:", data_to_send) await self.send(text_data=json.dumps({"data": data_to_send})) await asyncio.sleep(1) except asyncio.CancelledError: pass But im getting default value that initializing in constructor so im cant reach values from on_message method in my consumer, any one can help me with it? -
Selenium within Anaconda
For some time I have been trying to figure out how to handle reading a few different programming books and having other projects. I learned that it would be best through a python virtual environment to manage the package dependencies. However I then needed/wanted to use the same python version. Which I found a version of pyenv for windows, now when I am within the local environments I am still having issues. I have decided to start using the open source program Anaconda to handle my separate projects and there environments. However, I am reading Test Driven Development with Python, Obey the testing GOAT! Which uses Selenium and Django, I have them installed however I am still greeted with another Error message: (django-selenium) C:\Users\Steven Walker\Documents\Projects\Python\tdd-book>python functional_tests.py Traceback (most recent call last): File "C:\Users\Steven Walker\Documents\Projects\Python\tdd-book\functional_tests.py", line 2, in <module> browser = webdriver.Firefox() File "C:\Users\Steven Walker\.conda\envs\django-selenium\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 170, in __init__ RemoteWebDriver.__init__( File "C:\Users\Steven Walker\.conda\envs\django-selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__ ...... I'm sure that I have the geckodriver for Firefox installed, as I am able to use geckodriver --version. I have added the firefox browsers directory to the PATH in environment variables but still no luck. I was wondering if anyone would be able … -
file empty after upload in django admin
I want to save a beforehand created pdf file to my django model in model's save method: class MyModel(BaseModel): pdf_file = models.FileField(upload_to='pdfs',blank=True, null=True) And my mdoel save: def save(self, *args, **kwargs): #create the file at pdf_path with fpdf self.pdf_file.save("myfile.pdf" , open(pdf_path, "r", encoding='ISO8859-1'), save=False) super().save(*args, **kwargs) There is no errors, the file is created at pdf_path and is a 5 page file with content. There is also a file uploaded into the django database but it is an empty 5 page pdf file. -
Django Multi OR Operations
I am trying to do an OR operation for a specific group without knowing the number for each so that it is more than an OR LIKE SELECT * FROM movies WHERE genres LIKE '%Action%' or genres LIKE '%Comedy%' If we assume that I have genres added to it, how can I control the genre so that more than one OR can be chosen for genres without knowing how many genres will be entered? Like Example User input Genres: [Action, Comedy, Horror] as we can see user add new Genres Horror so, my query Muse be SELECT * FROM movies WHERE genres LIKE '%Action%' or genres LIKE '%Comedy%' or genres LIKE '%Horror%' My Simple Script series = series_db.objects.filter(Q(genres__icontains=genres), status=1 ).values('id','title') -
Django Page not found (404) No order item matching the query was found
I'm new to Django and doing my first serious project Why Django returns the error: "Page not found (404) No order item matching the query"? enter image description here application view orders The DataMixin class is used to generate the contents of the main menu. It is used in almost all other views of my application, so it seems to me that the problem is clearly not in it view queryset returns the expected set, despite the fact that Django writes in error <QuerySet [<OrderItem: 31>, <OrderItem: 32>, <OrderItem: 33>, <OrderItem: 34>]> def get_queryset(self): queryset = OrderItem.objects.filter(order_id=self.kwargs['order_id']) print(queryset) return queryset class OrderDetail(DataMixin, DetailView): model = Order template_name = 'orders/order_detail.html' context_object_name = 'order' pk_url_kwarg = 'order_id' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) c_def = self.get_user_context() return context | c_def | {'title': 'Заказ'} def get_queryset(self): queryset = OrderItem.objects.filter(order_id=self.kwargs['order_id']) return queryset main_urls I suspect Django doesn't see my url for some reason. urlpatterns = [ path('admin/', admin.site.urls), path('', include('home.urls', namespace='home')), path('cart/', include('cart.urls', namespace='cart')), path('orders/', include('orders.urls', namespace='orders')), path('users/', include('users.urls', namespace='users')), ] urls app_name = 'orders' urlpatterns = [ path('order/<int:order_id>/', OrderDetail.as_view(), name='order_detail'), path('create/', OrderCreateView.as_view(), name='order_create'), ] models class Order(models.Model): username = models.CharField(max_length=100, verbose_name='Логин') first_name = models.CharField(max_length=50, verbose_name='Имя') last_name = models.CharField(max_length=50, verbose_name='Фамилия') email = models.EmailField() address … -
Celery beat doesn't pick up tasks
I have a task that is supposed to run every minute and a celery worker. I have placed the celery.py into my main project folder, where wsgi.py is located and it looks like this: from __future__ import absolute_import, unicode_literals import os from celery import Celery from django.conf import settings os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TestTask.settings') app = Celery('TestTask') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks(settings.INSTALLED_APPS) @app.on_after_configure.connect def setup_periodic_tasks(sender, **kwargs): sender.add_periodic_task(60.0, print_hw(), name="print_hw") @app.task def print_hw(): print('hello_world') app.conf.beat_schedule = { 'print_hello_world': { 'task': 'tasks.print_hw', 'schedule': 10.0, } } app.conf.timezone = "UTC" But when I run the command celery -A TestTask beat I only see the following in the console: celery beat v5.3.1 (emerald-rush) is starting. __ - ... __ - _ LocalTime -> 2023-08-30 14:20:04 Configuration -> . broker -> redis://127.0.0.1:6379/0 . loader -> celery.loaders.app.AppLoader . scheduler -> celery.beat.PersistentScheduler . db -> celerybeat-schedule . logfile -> [stderr]@%WARNING . maxinterval -> 5.00 minutes (300s) And nothing happens afterwards, i.e. tasks aren't being identified/executed by the worker. Any idea what I did wrong here or what can be the problem? -
how to integrate django and scrapy and celery?
I have a django project and in its root directory i have a scrapy project that has a spider that should be run every 24 hours and it scrapes provided urls and saves data to a mysql database using django model. i want to shcedule this task using celery beat but i don't know how to integrate django and scrapy project. this is how my project tree looks like: news-website | ├──django_app | └──models.py | ├──news-website | └──settings.py (and other django auto made files are in this directory) | ├──manage.py ├──tasks.py | └──scrapy_project ├──scrapy_project | ├──settings.py | ├──piplines.py | ├──items.py | └──spiders | └──spider.py └──scrapy.cfg i tried to create a file in root directory named as tasks.py and i tried to run my spider in it but when i run tasks.py i get this error. from scrapy_project.items import NewsItem ModuleNotFoundError: No module named 'scrapy_project.items' actually i have this line in my spider.py file: from scrapy_prject.items import NewsItem but since i am running my spider from a script it seems that items file is not accessible for it. how should i fix this problem and how should i use celery with this project for scheduling my scraper? -
Django Rest Framework Role and Permissions having hierarchical relationship
We have two user types: service provide customer each user has following roles- admin service and support operator -reporter but each role has follwoing subrole region admin, region operator, region service and support and with same way to other roles site admin, site operaotor , site service and support in above case service provider and cusotmer has hierarchical relationship. service provider in top of customer. then how to manges role and permission with hierarchical in efficent way ? how to get data in hierarchial way ? then how to manges role and permission with hierarchical in efficent way ? how to get data in hierarchial way ? -
How to serve django static files via kong and nginx
I have Django app, nginx and Kongo. I configured the service and routes. I have a simple web page with a form. In order to display it correctly, you need to load static files such as js, css, png. I don't understand how to tell Kongo routes to static files. django nginx kong deployed in docker. Didn't find any information on how to do it. -
How to integrate OTP sending with Celery into a project?
Here is my viewset and serialsers. I want to integrate OTP sending during user authorization. To pass successful authorization, you need to enter a 6-digit OTP code. How can I do it better? I also want to connect Celery to send OTP codes. from djoser.views import UserViewSet class AccountViewSet(UserViewSet): queryset = User.objects.all() search_fields = ('^username', '^email', '^first_name', '^last_name') from djoser.serializers import UserCreateSerializer as UserCreate from djoser.serializers import UserSerializer class CustomUserCreateSerializer(UserCreate): password = serializers.CharField( max_length=MAX_LENGTH_PASSWORD, required=True, write_only=True, validators=[ MinLengthValidator( limit_value=MIN_LENGTH_PASSWORD, message='Password must be longer than 6 characters' ), MaxLengthValidator( limit_value=MAX_LENGTH_PASSWORD, message='Password must be shorter than 30 characters' ), validate_password ], help_text=( 'Enter password.' 'It must include lowercase and uppercase letters ' 'of the Latin alphabet and numbers.' ), ) email = serializers.EmailField( validators=[ UniqueValidator( message='This email is already taken', queryset=User.objects.all() ), MaxLengthValidator( limit_value=MAX_LENGTH_EMAIL, message=( 'Email must be shorter than' f'{MAX_LENGTH_EMAIL} characters' ) ) ], required=True ) username = serializers.CharField( validators=[ UniqueValidator( message='This username is already taken', queryset=User.objects.all() ), validate_username ], required=True ) first_name = serializers.CharField( max_length=MAX_LENGTH_USERNAME, help_text='Enter your first name', validators=[validate_first_name], required=True ) last_name = serializers.CharField( max_length=MAX_LENGTH_USERNAME, help_text='Enter your last name', validators=[validate_last_name], required=True ) class Meta(UserCreate.Meta): model = User fields = ( 'id', 'email', 'first_name', 'last_name', 'username', 'password' ) class CustomUserSerializer(UserSerializer): … -
Django Image upload missing exif data on Android Chrome devices
I'm having some issues with a project I'm working on at the moment. In testing on my local machine there were no issues, on production there are no issues via a desktop PC but I am experiencing issues on Android Chrome which will be the main use for the application. Essentially I have a simple form in django that takes a few values and has an Image Upload field. I then have the following piece of code at the start of the submission: if request.method == "POST": form = ImageUploadForm(request.POST, request.FILES) image = request.FILES.get('image') print(image) try: x = float(getgpsloc(image)[0]) y = float(getgpsloc(image)[1]) The process will upload the file, take the GPS and some other exif data and if valid submit some of this information to the database and at the same time strip the exif data from the image and rename the file so there is no user identifiable information. As noted, on PC this works fine, the getgpsloc function reads all of the exif fields as expected. On Android Chrome however it appears as though only a couple of the exif data fields are present in the image file that is then causing a divde by zero error as … -
Why is running django app in cmd giving me a module error?
each time I try to run my django app it is giving me this error( ModuleNotFoundError: No module named 'django.contrib.genresdjango') The genres is the page iam trying to create I tried removing the line from the django settings.py it is running perfectly but when i add the line for my app it is not -
How can I check what is the maximum python version that I use with in my Django?
I'm using Django with many pip modules; python version is 3.7.5. I want to write a script that will tell me at any given moment what is the maximum python version that I can use without pip module changes. Thank you. -
Fetching Captions from CustomImage Model for Images in Wagtail RichText
I'm working on a Wagtail project and I'm trying to dynamically fetch captions from a CustomImage model to display below images within a RichText field. However, I've encountered challenges in implementing this functionality correctly. In my project, the rich text field is loaded as follows: <div class="blog_detail_content"> {{ page.body }} </div> the images used within the RichText field are loaded as follows on the browser: <img alt="alt_Text" class="richtext-image full-width" height="400" src="/media/images/discovering_e-paath_in_canada_5.width-800.jpg" width="800"> The CustomImage model is related to the images used in the default richtext field provided by Wagtail. Desired Outcome: I want to dynamically fetch captions from the CustomImage model and display them below the images in the RichText field. What I've Tried: I've attempted to implement this by using JavaScript and creating API endpoints, but I'm facing difficulties in correctly associating images with their respective captions. Code Samples: #custom images model class CustomImage(AbstractImage): alt_text = models.CharField(max_length=255, blank=True, null=True, help_text="alt text for the image") caption = models.TextField(blank=True, null=True, help_text="caption for the image") admin_form_fields = Image.admin_form_fields + ( 'alt_text', 'caption' ) class CustomRendition(AbstractRendition): image = models.ForeignKey(CustomImage, on_delete=models.CASCADE, related_name='renditions') class Meta: unique_together = ( ('image', 'filter_spec', 'focal_point_key'), ) #views to retrive the caption from the customimagemodel with provided image **def get_caption(request, … -
Switching to a web framework from vanilla WSGI
Our stack split into four main components: Several web applications that contain only the presentation layer Another application that we called Warden that contains a set of WSGI endpoints that we define ourselves. A library of classes that represent our database entities. A PostgreSQL database In order for the web applications to make requests to the database, they talk to the Warden. We have realised that our user's needs are such that we need to write endpoints often and continually. The fact that we explicitly write our endpoints means that the turn around time from getting a request from a user to implementing is very slow. Is it possible to rewrite our Warden in a web framework? Is there a framework that is particularly suited to this? Alternatively, is there a different and better approach to this problem? -
deplpoy django app to azure cloud with debug = 0
I have depployd a django app to the azure cloud environment. But I still see the message: You’re seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed. But I have set the debug to 0: settings.py: SECRET_KEY = os.environ.get('SECRET_KEY') DEBUG = bool(int(os.environ.get('DEBUG', 0))) ALLOWED_HOSTS = ['https://docker.azurewebsites.net'] ALLOWED_HOSTS.extend( filter( None, os.environ.get('ALLOWED_HOSTS', '').split(" "), ) ) CSRF_TRUSTED_ORIGINS = ["https://docker.azurewebsites.net"] And if I run the container on localhost. It works. I don't see the extra information. Just output: Not Found The requested resource was not found on this server. But when I enter the url https://site.azurewebsites.net/admin/login/?next=/admin/ Then the information is shown: Help Reason given for failure: Origin checking failed - https://docker.azurewebsites.net does not match any trusted origins. In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django’s CSRF mechanism has not been used correctly. For POST forms, you need to ensure: Your browser is accepting cookies. The view function passes a request to the template’s render method. In the template, there is a {% csrf_token %} template tag inside each POST form that targets … -
OVERRIDING THE ORDER OF FIELDS IN SERIALIZER RESPONSE
class Meta: model = Order fields = ( 'brand', 'currency', 'order_id', 'reference_id', 'ordered_date', 'amount', 'quantity', 'order_status', 'app_code', 'vouchers') I need to change the ordering of field's on response. I have changed the ordering in class Meta. its not working i have tried to_representation method. -
Djang - 404 Error occurs only at specific time
I'm running my Django project in a docker container on AWS EC2 instance. There are some APIs and all of them have been working fine for several months. But since last week, APIs return 404 error only around 6pm everyday. <head><title>404 Not Found</title></head> <body> <center><h1>404 Not Found</h1></center> <hr><center>openresty/1.19.9.1</center> </body> </html> Is there any way to solve this error? Any comments would be appreciated. -
costum variable index inside a DJango template
I have a template in Django that is generated by a for loop like {% for test in test_list %} <!-- dome code here --> {% if not test.testInPausa %} <div class="progress-wrapper-{{ forloop.counter0 }}"> <div id="progress-bar-{{ forloop.counter0 }}" class="progress-bar-{{ forloop.counter0 }} progress-bar-striped" > &nbsp; </div> </div> <div id="progress-bar-message-{{ forloop.counter0 }}"> Waiting for progress to start... </div> {% endif %} {% endfor %} Now, I would like to count the time in which the code enter the if statement. I want to do so because, at the moment, the id and class inside the div are indexed by the forloop.counter, which, because it doesn't always enter the if statement, counts every for loop making the id and class jump index. Is there a way to have something {% for test in test_list %} {% my_index = 0 %} <!-- dome code here --> {% if not test.testInPausa %} <div class="progress-wrapper-{{ my_index }}"> <div id="progress-bar-{{ my_index }}" class="progress-bar-{{ my_index }} progress-bar-striped" > &nbsp; </div> </div> <div id="progress-bar-message-{{ my_index }}"> Waiting for progress to start... </div> {% my_index++ %} {% endif %} {% endfor %} -
Read data from dynamic table Django
I have automated batch processn creates tables in sqlite DB and update the table in one of the table defined by Django ORM. I would like to use Django ORM to select data from newly created DB. -
Charts not loading out data for my django project
So, I've been trying to get my charts to display packages created per hour, but it doesn't render out the data on the chart. The axes display though. When I use print statements in the view, it returns an empty array for the hours, despite the same code working for a different project def warehouse_dashboard(request): user = request.user if user.role == 'warehouse_admin': warehouse = user.warehouse_admin_field if warehouse: buses = Bus.objects.filter(warehouse=warehouse) # Filter items that are either assigned to a bus or have a delivery status of "Ready" packages_coming = Item.objects.filter( Q(station_to=warehouse, bus_number_plate__isnull=False) | Q(station_to=warehouse, delivery_status='Ready') ).exclude(delivery_status__in=['Canceled', 'Received', 'Ready']) packages_leaving = Item.objects.filter(station_from=warehouse).exclude(delivery_status='Canceled') packages_by_bus = {} for bus in buses: packages_from = Item.objects.filter( Q(station_from=warehouse) | Q(station_to=warehouse), bus_number_plate=bus ).exclude(delivery_status='Canceled') packages_by_bus[bus] = packages_from bus_count = buses.count() packages_count_coming = packages_coming.count() packages_count_leaving = packages_leaving.count() # Retrieve data for the graph (daily packages added) # Get the datetime for the start of the current day current_day_start = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) hourly_totals = Item.objects.filter( station_from=warehouse, creation_date__gte=current_day_start ).annotate(hour=ExtractHour('creation_date')).values('hour').annotate(total=Count('id')).order_by('hour') # Initialize an array to hold the data for each hour of the day chart_data = [0] * 24 for total in hourly_totals: hour = total['hour'] if hour is not None: chart_data[hour] = total['total'] print(chart_data[hour]) # Get the number of … -
Permission denied for ..../<id>/ CRUD ModelViewSet endpoints
I am trying to create a ModelViewSet for supporting CRUD operations on my address model. I am able to successfully use LIST and CREATE endpoints, but all the other 4 endpoints which require/id/ in their URL return me the error - { "detail": "You do not have permission to perform this action." } The address model should support the operations only by the Users who are marked NOT SELLER in the DB, hence the ~IsSeller custom permission is applied. The issue goes away when I remove this custom permission from permissions classes, but i need to keep this restriction of only non-seller users to be able to add the address. permissions.py from rest_framework.permissions import BasePermission class IsSeller(BasePermission): def has_permission(self, request, view): return (request.user and request.user.is_seller) Views.py class ManageAddressViewSet(viewsets.ModelViewSet): serializer_class = AddressSerializer permission_classes = [permissions.IsAuthenticated, (~IsSeller)] queryset = Address.objects.all() def get_queryset(self): return self.queryset.filter(user=self.request.user) def perform_create(self, serializer): return serializer.save(user=self.request.user) Serializers.py class AddressSerializer(serializers.ModelSerializer): class Meta: model = Address fields = [ 'id', 'name', 'line_1', 'line_2', 'city', 'state', 'pincode', 'type' ] read_only_fields = ['id'] urls.py from django.urls import path, include from rest_framework.routers import DefaultRouter from user import views router = DefaultRouter() router.register('address', views.ManageAddressViewSet) urlpatterns = [ path('create/', views.CreateUserView.as_view(), name='create'), path('authenticate/', views.CreateTokenView.as_view(), name='authenticate'), path('my-profile/', views.ManageUserView.as_view(), …