Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
django - upload page should be accessable after form submission only once. Directly entering the upload url should not work
i can upload a file after the form is submitted. The upload url should not be accesssable otherwise After the user logins, the upload url is ascessed directly. How to restrict this. This will create multiple entries of file upload for the same form views.py Data1 = request.POST["Data1"] Data2 = request.POST["Data2"] Data3 = request.POST["Data3"] return redirect(uploaddata)``` ```def uploaddata(request): if request.user.is_authenticated: if request.method == 'POST': form = uploadmetaform(request.POST, request.FILES) if form.is_valid(): post = form.save(commit=False) post.user = request.user #post.tar_gif = request.FILES['tar_gif'] post.save() return redirect('file_list') else: form = uploadmetaform() return render(request, 'uploaddata.html', { 'form': form }) else: return render(request, 'home.html')``` -
Location for app-wide utils file in a Django project
Given the nightmare of relative imports, where should I put a simple utils.py file in a Django project that has multiple apps (accessible within the models.py, admin.py, views.py all files of all app directories)? It seems like there still isn't a good answer for this in 2019, even though Django is by far the most popular framework of the language. Considering Python 2 will be sunset in January of 2020, please also provide an answer for Python 3. -
Problem with save OneToOne relation when user is register
I want to save User and also user details in additional date when user is register by rest api. When User is saved I want to save this additionality data to userDetails tabel with User ForeingKey. This should be by RegisterSerializer. Im using django-rest-auth. I have models: class User(AbstractUser): # First Name and Last Name do not cover name patterns # around the globe. name = CharField(_("Name of User"), blank=True, max_length=255) is_partner = BooleanField(_('User is partner'), default=False) def get_absolute_url(self): return reverse("users:detail", kwargs={"username": self.username}) class UserDetails(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) address = models.CharField(max_length=200, blank=True) city = models.CharField(max_length=100, blank=True) zip_code = models.CharField(max_length=20, blank=True) country = models.CharField(max_length=30, blank=True) def __str__(self): return self.user.username and serializers class UserDetailsSerializer(serializers.ModelSerializer): user = UserSerializer(read_only=True) class Meta: model = UserDetails fields = "__all__" class UserRegisterSerializer(RegisterSerializer): is_partner = serializers.BooleanField(default=False) address = serializers.CharField(max_length=200) city = serializers.CharField(max_length=100) zip_code = serializers.CharField(max_length=20) country = serializers.CharField(max_length=30) def get_cleaned_data(self): super(UserRegisterSerializer, self).get_cleaned_data() return { 'password1': self.validated_data.get('password1', ''), 'email': self.validated_data.get('email', ''), 'name': self.validated_data.get('name', ''), 'address': self.validated_data.get('address', ''), 'city': self.validated_data.get('city', ''), 'zip_code': self.validated_data.get('zip_code', ''), 'country': self.validated_data.get('country', ''), } -
How to combine ListView and UpdateView
In my web application, I have a template with ListView that shows list of table records, that works fine. Now I want to allow user to update fields for each row of the table. One "easy" solution is to create an update page using UpdateView and put a link to it in each row. User can then click update, open update page ... But I am wondering if there is a way to update fields in the same table, without opening a new page. Hope my question is clear. thanks for your help -
celery start multiple workers and one beat as daemon crash
i try to start multiple workers with celery multi and celery beat simultaneously. I start the workers as daemon with this command: celery multi start 2 -A djangoproj -Q:1 longtaskqueue -Q:2 shorttaskqueue -c:1 1 -c:2 1 Now if i start the beat as daemon with: celery multi start -A djangoproj workerbeat1 --beat the workers crash with this error in the log file: [2019-11-06 13:00:51,641: WARNING/MainProcess] /usr/local/lib/python3.7/dist-packages/celery/fixups/django.py:202: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments! warnings.warn('Using settings.DEBUG leads to a memory leak, never ' [2019-11-06 13:01:16,722: ERROR/MainProcess] Control command error: OperationalError("\nCannot route message for exchange 'reply.celery.pidbox': Table empty or key no longer exists.\nProbably the key ('_kombu.binding.reply.celery.pidbox') has been removed from the Redis database.\n") Traceback (most recent call last): File "/usr/local/lib/python3.7/dist-packages/kombu/connection.py", line 439, in _reraise_as_library_errors yield File "/usr/local/lib/python3.7/dist-packages/kombu/connection.py", line 518, in _ensured return fun(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/kombu/messaging.py", line 203, in _publish mandatory=mandatory, immediate=immediate, File "/usr/local/lib/python3.7/dist-packages/kombu/transport/virtual/base.py", line 605, in basic_publish message, exchange, routing_key, **kwargs File "/usr/local/lib/python3.7/dist-packages/kombu/transport/virtual/exchange.py", line 70, in deliver for queue in _lookup(exchange, routing_key): File "/usr/local/lib/python3.7/dist-packages/kombu/transport/redis.py", line 877, in _lookup exchange, redis_key)) kombu.exceptions.InconsistencyError: Cannot route message for exchange 'reply.celery.pidbox': Table empty or key no longer exists. Probably the key ('_kombu.binding.reply.celery.pidbox') has been removed from the … -
Original exception text was: 'int' object has no attribute 'product'
When I am going to pass list objects in url I am getting this error Got AttributeError when attempting to get a value for field `product` on serializer `ProductForParameterSerializer`. The serializer field might be named incorrectly and not match any attribute or key on the `int` instance. Original exception text was: 'int' object has no attribute 'product'. ` class ProductParameter(models.Model): product_attribute = models.ForeignKey(ProductAttribute, on_delete=models.CASCADE) product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='product_parameters') parameter = models.CharField(max_length=100, default='-') and serializers.py looks like this class ProductForParameterSerializer(serializers.ModelSerializer): name = serializers.CharField(source='product.name', max_length=255, read_only=True) product_desc = serializers.CharField(source='product.description', max_length=255, read_only=True) image = serializers.ImageField(source='product.image', read_only=True) price = serializers.DecimalField(source='product.price', max_digits=10, decimal_places=2, read_only=True) rating = serializers.CharField(source='product.rating', max_length=10, read_only=True) class Meta: model = ProductParameter fields = ('id', 'product', 'name', 'price', 'product_desc', 'image', 'rating') as you can see in the model there is Product ForeignKey. In this model there may be more than one product and from this table I should get product which its id is unique. I do not need duplicate products. and for this I am using this view class ProductForParameterView(generics.ListAPIView): serializer_class = ProductForParameterSerializer def get_queryset(self): query_params = self.request.query_params products = query_params.get('product', None) productParams = [] if products is not None: for product in products.split('|'): productParams.append(int(product)) if products is not None: queryset … -
Outer join produced by exclude filter slowing down query enormously
Alright, so we have a sports Season which belongs to a League which belongs to an Association. A season is made up of Matches. On the association page we want to display non-empty current seasons (with matches): class SeasonManager(models.Manager): def current(self): return self.get_queryset().filter(enddate__gte=date.today()).exclude(match=None) And then we defining Association.current_seasons as Season.objects.current().filter(league__association=self). Problem: the last exclude filter creates a massive outer join making the query run for about 15 seconds... I've changed to the following which runs as fast as you'd expect: class SeasonManager(models.Manager): def current(self): seasons = self.get_queryset().filter(enddate__gte=date.today()) return Season.objects.filter(id__in=[s.id for s in seasons if s.match_set.exists()]) I wonder if there's a better way to do it, especially prefetching the "existence" of a season match set, or rather running an intermediate query evaluating that existence. I've tried the following just in case that does the trick but it obviously doesn't: self.get_queryset().filter(enddate__gte=date.today()).exclude(match=None).prefetch_related('match_set__id') -
Cloudinary Image upload from django
I am trying to upload image/file to clodinary to get back the url using this code medical_file = request.FILES['medical_file'] out = cloudinary.uploader.upload(File.open(medical_file, "rb")) url = out['url'] medical_file_url = url And I am successfully getting the url as well(I have printed that on my console) But then I am getting this error of : cloudinary.api.Error: Empty file -
Django channels working with ws:// but fails to handshake with wss://
nginx setting server { listen 80 default_server; listen [::]:80 default_server; server_name www.sample.co; return 301 https://$host$request_uri; } server { ssl on; listen 443 ssl; listen [::]:443 ssl; server_name www.sample.co; rewrite ^(.*) https://beta.sample.co$1 permanent; ssl_certificate /home/ubuntu/certificate/certificate.crt; ssl_certificate_key /home/ubuntu/certificate/private.key; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } } Running on daphne CMD ["daphne","-b","0.0.0.0","-p","8001","config.asgi:application"] Working fine on ws://xyz.amazonaws.com/ws/sample// Fails to work on wss://xyz.amazonaws.com/ws/sample// Error: WebSocket connection to 'wss://xyz.amazonaws.com/ws/sample//' failed: Error in connection establishment: net::ERR_CERT_COMMON_NAME_INVALID The applications are running on AWS. The websocket is getting called from https. I tried to call the websocket with ws:// from a dummy react app in local it is working fine. -
Upload csv files with unknown columns in a database in django
I'm working on an app which has the following steps: 1. Select a csv file from local machine (user's input) 2. read the file and store it in a sqlite database with the table name "data". If a database already exists, overwrite it with this new data I've seen a couple of tutorials but none seem to answer how to do the second part. Anyone who has done it before or may have an idea as to how to do it? -
Datetime comparison with a timedelta from an F() object
Context : I'm trying to do a condition for a queryset filtering. It implies 2 fields of my model and datetime.datetime.now() (aka. now), one field representing a time delta (a number of minutes) and one field representing a datetime (start_datetime). I'd like to check if start_datetime - now <= time_delta`. General case : I've seen the example (from the doc) in which we're able to compare two date(time)s and a constant timedelta. >>> from datetime import timedelta >>> Entry.objects.filter(mod_date__gt=F('pub_date') + timedelta(days=3)) But what if it's a value contained in an other field of my entry? The way I try to instanciate this timedelta is obviously not possible. >>> from datetime import timedelta >>> Entry.objects.filter(mod_date__gt=F('pub_date') + timedelta(days=F('n_timedelta'))) Could there be a solution to achieve this using the ORM features (and not a raw SQL query)? -
Page is opening but search is not working when i am using {% for i in kitty_list %} but in case of {% for i in kitty %} then NoReverseMatch error
// In Below scenario page is opening but search functionality not working when i am using {% for i in kitty_list %} in the template but when i am using {% for i in kitty %} then NoReverseMatch error is coming Url: path('kitty_view',views.kitty_view,name='kitty_view') View: kitty_list = kitty_list.filter(status = status1) kittys = kitty.objects.all() ctx = {'kitty': kitty_list,'kitty_code':kittys} return render(request, 'kitty/kitty_view.html', ctx) //Html page which is giving NoReverseMatch error when {% for i in kitty %} is used. When i am using {% for i in kitty_list %} then page is rendering but serach button is not working template: {% extends 'base.html' %} {% load static %} {% block content %} <form class="form-signin" action="{% url 'kitty_view' %}" method="get"> {% csrf_token %} <div class="form-row"> <div class="mb-3"> <select class="custom-select center-block" name="code1" id="code1"> <option value="">Choose Kitty...</option> {% for j in kitty_code %} <option value="{{ j.code }}"> {{ j.code|add:' - '|add:j.name }} </option> {% endfor %} </select> </div> <div class="mb-3"> <input type="text" name="nam" id="nam" class="form-control-sm center-block" placeholder="Name" autofocus> </div> <div class="mb-3"> <select class="custom-select center-block" name="stat" id="stat" placeholder="Status"> <option value="">Choose Status...</option> <option>A</option> <option>I</option> </select> </div> <div class="mb-3"> <button type="submit" class=" btn btn-info " role="button">Search</button> </div> </div> </form> <table class="table table-dark"> <thead> <tr> <th scope="col">#</th> <th scope="col">Kitty Code</th> … -
Find file in templates directory
In versions up to 2.2.5 of Django I put my html files that were passed in views as follows: myapp templates myapp index.html But in django 2.2.6 and 2.2.7 when I do this gives the following error: TemplateDoesNotExist at / But when I do: myapp templates index.html The template is found. How in these newer versions does Django handle to identify 2 templates with the same name without needing subdirectory to differentiate? -
Creating DetailApiViewset by using classed based api view
I am trying to create detail api viewset but for now I am getting page not found whenever I go to my "matches/api/matches/1/". ListCreateAPIView works fine. Here are my viewsets: class MatchViewset(viewsets.ViewSetMixin, generics.ListCreateAPIView): serializer_class = MatchSerializer def get_queryset(self): header_token = self.request.META.get('HTTP_AUTHORIZATION', None) print(header_token) if header_token is not None: try: token = sub('Token ', '', self.request.META.get('HTTP_AUTHORIZATION', None)) token_obj = Token.objects.get(key=token) self.request.user = token_obj.user except Token.DoesNotExist: pass print(self.request.user) return Match.objects.filter(creator=self.request.user) class MatchDetailViewset(generics.RetrieveUpdateDestroyAPIView): queryset = Match.objects.all() serializer_class = MatchSerializer and routers: router = routers.DefaultRouter() router.register(r'matches', matches_views.MatchViewset, base_name="matches") Thanks for all answers :) -
dajngo-admin-sortable2 doesn't sort order of SortableAdminMixin
I created an app with django and python3 containing several tutorials, which in turn contain multiple content entrys that are saved in another table. Each tutorial is represented by an entry in the exercises tutorial model. I want to be able to sort the tutorials and the contents in the django admin panel. It works fine for the inline tutorial contents with SortableInlineAdminMixin class. It also works fine if I create new tutorials from scratch without having any objects saved before (it works local on my pc if I clone this project and set it up from scratch) My problem now is, that I have the app setup like you see in the code and pictures below on a ubuntu server with apache, but I can't sort the tutorials (the inline contents still work fine). If i drag and drop them to a new position and reload, they don't save at their new position and fall back to their old position. Tutorial and TutorialContent model: from django.db import models from ckeditor_uploader.fields import RichTextUploadingField # Page for tutorials class Tutorial(models.Model): title = models.CharField(max_length=60) order = models.PositiveIntegerField(default=0, null=False, unique=True) # Timestamp created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ['order'] … -
How can I fix django Reverse function to work with the current application?
I have little experience with django and APIs and recently started developing this application. My code structure goes like this: config/ env/ .env files requirements/ __init__.py ... settings/ __init__.py base.py local.py __init__.py urls.py views.py wsgi.py projects/ api/ town/ apps.py urls.py views.py ... core/ models/ town.py serializers/ town_serializer.py ... tests/ test_town.py manage.py this is the file project/api/town.urls.py from django.urls import path from . import views app_name = 'town' urlpatterns = [ path('town/', views.CreateTownView.as_view(), name='town-list'), path('town/<int:pk>/', views.TownViewSet.as_view(), name='town-detail') ] And the file for config/settings/urls.py: from django.contrib import admin from django.urls import path, include # from .views import views urlpatterns = [ path('admin/', admin.site.urls), # path('', views.index) path('api/', include('project.api.city.urls', namespace='city')), path('api/', include('project.api.town.urls', namespace='town')) ] I have used routes like localhost:8000/api/town and I had no problem working with API and they're working just fine. The only problem happans when I am trying to run the test module. This is the part of test that error pops up:-{ from django.db import IntegrityError from django.test import TestCase from project.core.models.town import Town from django.urls import reverse from rest_framework.test import APIClient from rest_framework import status import unittest CREATE_TOWN_URL = reverse('town:town-list') DETAIL_TOWN_URL = reverse('town:town-detail') this is the error: raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'town-detail' with no arguments not found. … -
Accessing server cookies from localhost
I have a dev server at http://dev.example.com. My react app is hosted locally and I need to use the dev server. The problem is that the server cookies have the domain 'dev.example.com' which is not accessible from localhost. I can use a proxy server and set Nginx to proxy_pass to the dev server but I want a simpler solution without a proxy server. What can I do? -
Get Redis client_list from Django console
I have error max number of clients reached. And I want to see who are these all clients (not web app only). I found that Redis have command for that named client_list(). But I was unable to use it from Django shell. from django.core.cache import cache cache.get_list() # Raises: # TypeError: 'list' object is not callable cache.client_list # Returns: # [StrictRedis<ConnectionPool<Connection<host=somehost.amazonaws.com,port=12345,db=1>>>] What is correct way to retrieve the list? -
Django Bulk upload of Large Files
I want to upload a xlsx file to my database.I tried upload a file using Openpyxl package and bulk_create option in the django as given below, model.py, class cardata(models.Model): Name= models.CharField(max_length = 60, db_column = 'Name') Model_Type= models.CharField(max_length = 60, db_column = 'Model_Type') ...... objects = models.Manager() pdobjects = DataFrameManager() def __str__(self): field_values = [] for field in self._meta.get_fields(): field_values.append(str(getattr(self, field.name, ''))) return ' '.join(field_values) views.py, def upload(request): if request.method == "POST": excel_file = request.FILES["excel_file"] wb = openpyxl.load_workbook(excel_file, data_only=True) worksheet = wb.worksheets[0] excel_data = list() for row in worksheet.iter_rows(): row_data = list() for cell in row: if cell.value=='': cell.value='None' row_data.append(str(cell.value).strip()) excel_data.append(row_data) df = pd.DataFrame(columns=excel_data[0], data=excel_data[1:]) df_records = df.to_dict('records') model_instances = [cardata( Name =record['Name'], Model_type =record['Model_type'], Year =record['Year'], Make =record['Make'], ....... ) for record in df_records] cardata.objects.bulk_create(model_instances) return render('success.html') else: return render('upload_form.html') Above code works fine for the upload file having lesser data.When I try to upload a file having more than 5 lacks rows,it throw me the, MemoryError at /upload/ I have to deal with the large datasets. Suggest me a easier and fastest way to upload the data into database model or some codes. Thanks in advance. -
python_socketio with django cause db connection unclosed problem
Backgroud: django + Postgres + socketio Description: python_socketio: I create sync socketio to process some functions, and it will create a persistent thread to process the sockerio events django: in a django handle function ,I call socketio method, such as: class MyDangoHandler(object): def myfunction(self): socket_io_client.emit( event=ClientEvents.MyEvent, data={ "id": 1 } ) and in the sockerio event handler, I will call postgres database to get some data. We know that in a request, django will close the old db connections first,we suppose the main thread is Thread-1, then in the Thread-1, we call the socketio function, it is Thread-2, because of the db connection is threadlocal, so the closed_old_connections methods is unusable for db connections in sockertio(Thread-2), so after the connection created for a long time(> CONN_MAX_AGE), the connections in Thread-2 may still open, but the connection is unusable, it will cause the problem OperationError: server_closed_the_connection_unexpectedly This_probably_means_the_server_terminated_abnormally before_or_while_processing_the_request. Now I solve the problem by closing the old DB connections in socketio handler functions manually, but I don't think it's good enough because we need to close the connections in each handler which will call the database function. And I didn't find the root cause why the connections will be unusable if … -
Django - Insert youtube video in HTMLField
I'm trying to insert a youtube video on a Django HTMLField to look like an iframe, but i can't find the clue for it. Is there any way to insert it or a special field for youtube videos? Thanks! I tried to insert raw html and the link for the video -
How to make google, facebook authentication and token generation in django with flutter
I am using Flutter Google, Facebook login for authentication, i want to know which is best way to authenticate users in django with flutter login. also i want to generate auth token, so that django dont need to authenticate google, facebook login on every request from same user. -
Django: Filter against a value of another row
I'm having a model Foo with with a value field. # models.py class Foo(models.Model): value = models.IntegerField() Now I want to filter all Foo instances that have a greater value than the Foo object with a specific knwon id. pk = 10 obj = Foo.objects.get(pk=pk) qs = Foo.objects.filter(value__gte=obj.value) The question is can I somehow combine the two db queries above into one? -
How to allow google cloud storage to save duplicate files and not overwrite
I am using google cloud storage bucket to save file uploads from my Django web application. However if a file with same name is uploaded, then it overwrites the existing file and i do not want this to happen. I want to allow duplicate file saving at same location. Before moving to google cloud storage when i used my computer's hard disk to save files, Django used to smartly update filename in database as well as hard disk. -
How to authorize swagger ui using JWT Bearer Authorization in Django?
I am using JWT Authorization. Faceing Issue on authorize using Jwt Bearer Authorization.