Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django on Nginx serving static files but not media files when debug is False
My django app was running properly in production with debug set to True, when i turned it off, the app is not serving media files (uploaded by users) anymore, but the static files are served properly. here is my settings: STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, '../static/') STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),] MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads', 'media') MEDIA_URL = '/uploads/media/' DOCUMENTS_ROOT = os.path.join(BASE_DIR, 'uploads', 'media', 'documents') here are my base urls: urlpatterns = [ path('i18n/', include('django.conf.urls.i18n')), path('auth/', include("django.contrib.auth.urls")), path('traveller/', include('travellers.urls')), path('service/', include('drivers.urls')), path('admin/', include("management.urls")), path('events/', include('events.urls')), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) here is my Nginx site configuration: upstream app_server{ server unix:/home/envs/bus_env/run/bus.sock fail_timeout=10; } server { listen 80; # add here the ip address of your server # or a domain pointing to that ip (like example.com or www.example.com) server_name ***.***.***.***; keepalive_timeout 20; client_max_body_size 4G; access_log /home/envs/bus_env/logs/nginx-access.log; error_log /home/envs/bus_env/logs/nginx-error.log; location /static/ { alias /home/envs/bus_env/static/; } location /media/ { # media files, uploaded by users autoindex on; alias /home/envs/bus_env/backend/uploads/media/; } location / { try_files $uri @proxy_to_app; } location @proxy_to_app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; } } here is the nginx access log for when requesting a profile picture for example: "GET /uploads/media/staff/profile_pictures/viber_image_V2JdnkG.jpg HTTP/1.1" 404 799 this is my … -
Art with ID "1505/change/filetag" doesnt exst. Perhaps it was deleted?
class Art(BaseArt): ART_TYPES = ( ('USR', 'User'), ('APP', 'Application'), ('SYS', 'System'), ('DWN', 'Download'), ('NET', 'Network'), ) STATUS = ( ('queued', 'Queued'), # Queued for the admin to review ('flagged', 'Flagged'), # Flagged for the user to update ('updated', 'Updated'), # Updated by user, waiting for admin to re-review ('approved', 'Approved') # Approved. This should be changed automatically if "art.is_enabled" = true ) art_name = models.CharField(max_length = 50, help_text='Please use the following naming convention: < Object Type > < OS/System Type > < OS Version Num (if applicable) > < Art Specific Name >. EXAMPLE: File iOS 10.3.1 Wifi Connections') ###add method to get name/username art_type = models.CharField(max_length=10, choices=ART_TYPES, default='USR', help_text='The type of entity that created this art.') art_object_type = models.CharField(max_length=10, choices=ART_OBJECT_TYPES) date_added = CustomDateTimeField(default=timezone.now, blank=True) #datetime.now is_featured = models.BooleanField(default = False) user = models.ForeignKey(User, related_name='art_user', default=1,on_delete=models.CASCADE) ###Location? see notes... # Status reviewer = models.ForeignKey(User, related_name='reviewer', default=1, limit_choices_to={'is_staff':True},on_delete=models.CASCADE) # The admin assigned to review the art status = models.CharField(max_length=20, choices=STATUS, default='queued') # Approval state of the art (queued, flagged, updated, approved) points_awarded = models.BooleanField(default=False, help_text='Specifies if points have been awarded to the user and organization for this art. This field is automatically updated when an admin accepts an art, so this … -
How to check in django model ManyToManyField is symmetrical,if symmetrical=False?
I'm trying to create a followers system in Django. follows = models.ManyToManyField('self', related_name='follower', symmetrical=False, null=True, blank=True) Since the system should not be symmetrical, how can I check that two users follow each other? I want to have a function within the model, if possible. I might use this function within limit_choices_to of another field and some other places. My initial thought was to add a "friends" field and make it symmetrical, but this can bring other issues later. -
Bulk insertion in django rest framework
When I am trying to create multiple instances in a single request it shows error if one instance is not correct in the batch. But how can I write my create method to insert correct instances in the batch. That means only correct instances will insert in the db and also show the errors message for the wrong instances. [ { "name": "oil", "unit_price": 200 }, { "name": "meat", "unit_type": "raw", "unit_price": 1000 } "name": "salt", "unit_type": "raw", "unit_price": -100 } ] I want to insert first two instances will be insert in the db and for last one it will throws an error like this. "errors": [ { "unit_price": [ "Enter positive number." ] } ] Here is my serializer class ProductSerializer(serializers.ModelSerializer): def validate_unit_price(self, value): if (value) > 0: return value raise serializers.ValidationError("Enter positive number.") class Meta: model = Product fields = [ 'name', 'unit_type', 'unit_price'] Also my views function is @api_view(['POST']) def store(request): serializer = ProductSerializer(data = request.data,many=True) if serializer.is_valid(): serializer.save() return Response({'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'Something went wrong', 'data': request.data, 'errors': serializer.errors}) -
Websocket problem with [Errno 8] nodename nor servname provided, or not known
Desperately trying to understand what's going on here WebSocket HANDSHAKING /ws/lobby/ [127.0.0.1:65205] WebSocket CONNECT /ws/lobby/ [127.0.0.1:65205] Exception inside application: [Errno 8] nodename nor servname provided, or not known File "/anaconda3/lib/python3.7/site-packages/channels/sessions.py", line 183, in __call__ return await self.inner(receive, self.send) File "/anaconda3/lib/python3.7/site-packages/channels/middleware.py", line 41, in coroutine_call await inner_instance(receive, send) File "/anaconda3/lib/python3.7/site-packages/channels/consumer.py", line 59, in __call__ [receive, self.channel_receive], self.dispatch File "/anaconda3/lib/python3.7/site-packages/channels/utils.py", line 58, in await_many_dispatch await task File "/anaconda3/lib/python3.7/site-packages/channels/utils.py", line 50, in await_many_dispatch result = task.result() File "/anaconda3/lib/python3.7/site-packages/channels_redis/core.py", line 429, in receive real_channel File "/anaconda3/lib/python3.7/site-packages/channels_redis/core.py", line 484, in receive_single index, channel_key, timeout=self.brpop_timeout File "/anaconda3/lib/python3.7/site-packages/channels_redis/core.py", line 324, in _brpop_with_clean async with self.connection(index) as connection: File "/anaconda3/lib/python3.7/site-packages/channels_redis/core.py", line 820, in __aenter__ self.conn = await self.pool.pop() File "/anaconda3/lib/python3.7/site-packages/channels_redis/core.py", line 70, in pop conns.append(await aioredis.create_redis(**self.host, loop=loop)) File "/anaconda3/lib/python3.7/site-packages/aioredis/commands/__init__.py", line 175, in create_redis loop=loop) File "/anaconda3/lib/python3.7/site-packages/aioredis/connection.py", line 113, in create_connection timeout) File "/anaconda3/lib/python3.7/asyncio/tasks.py", line 388, in wait_for return await fut File "/anaconda3/lib/python3.7/site-packages/aioredis/stream.py", line 24, in open_connection lambda: protocol, host, port, **kwds) File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 899, in create_connection type=socket.SOCK_STREAM, proto=proto, flags=flags, loop=self) File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 1268, in _ensure_resolved proto=proto, flags=flags) File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 778, in getaddrinfo None, getaddr_func, host, port, family, type, proto, flags) File "/anaconda3/lib/python3.7/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/anaconda3/lib/python3.7/socket.py", line 748, in … -
how to override form widget of a model inline django
am attempting to load jsonSchema in JsonField in an inline model in django admin after 2 days searching i fond this solution .but the problem is ,even the script will load in JsonField but the main widget could not load into Field .i dont know what is the problem with my code. my main model: @admin.register(Product) class ProductModelAdmin(admin.ModelAdmin): form = ProductJSONModelAdminForm inlines = [productUnitInline, productImageInline] fieldsets = [ ('main ', {'fields': ['category', 'brand', 'name', 'values','description']}), ] change_form_template="content/my_product_admin.html" def get_formsets_with_inlines(self, request, obj=None): for inline in self.get_inline_instances(request, obj): if inline.model==ProductUnit: DATA_SCHEMA = { 'type': 'object', 'title': 'Data', 'properties': { 'color': { 'title': 'color', 'type': 'string', 'enum': ['red', 'blue', 'black'] }, 'size': { 'title': 'size', 'type': 'string', 'enum': ['L', 'M', 'S'] } }, "required": ['color', 'size'] } inline.form.base_fields['variant'].widget = JSONEditorWidget(DATA_SCHEMA, False) yield inline.get_formset(request, obj), inline def save_model(self, request, obj, form, change): super(ProductModelAdmin, self).save_model(request, obj, form, change) my inline : class productUnitInline(admin.StackedInline): model = ProductUnit form=productUnitJsonForm extra = 1 fields =["variant","variant_title","seller","price","storage_count"] my inline form: class productUnitJsonForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(productUnitJsonForm,self).__init__(*args, **kwargs) DATA_SCHEMA = { 'type': 'object', 'title': 'Data', 'properties': { 'color': { 'title': 'color', 'type': 'string', 'enum': ['red', 'blue', 'black'] }, 'size': { 'title': 'size', 'type': 'string', 'enum': ['L', 'M', 'S'] } }, … -
Django - Unable to delete old file on object change using signals
I have the following signal to delete the old postcover and postcover_tn (thumbnail) from my hard disk. This is working fine if I just delete the files trough my form and call save() but if I want to overwrite the old files with the new ones I upload the old ones are are still on my fs, any idea how to fix this?: signals.py @receiver(models.signals.post_save, sender=Post) def post_auto_delete_files_on_change(sender, instance, **kwargs): """ Deletes old file from filesystem when corresponding object is updated with new file. """ if not instance.pk: return False try: old_postcover = sender.objects.get(pk=instance.pk).postcover old_postcover_tn = sender.objects.get(pk=instance.pk).postcover_tn except sender.DoesNotExist: return False if not old_postcover: return new_postcover = instance.postcover if not old_postcover == new_postcover: if os.path.isfile(old_postcover.path): os.remove(old_postcover.path) os.remove(old_postcover_tn.path) postcover_tn gets generated on save() of Post, just if you might wonder about that. -
Django Instagram
I'm looking for some help on how to connect Instagram to my Django app. This article was helpful up to the point where Instagram moved to Facebook for configuring using the "Instagram App ID" and "Instagram App Secret" im getting a error ''' {"error_type": "OAuthException", "code": 400, "error_message": "Insufficient developer role"} ''' here is the article I followed: https://www.digitalocean.com/community/tutorials/django-authentication-with-facebook-instagram-and-linkedin facebook developer link: https://developers.facebook.com/ -
Django | Why use {% load static %} and make template complier work more
For static resources like images/CSS/Js I can use directly their complete path <link rel="stylesheet" href="/static/css/default.min.css"> #in app level templates: <link rel="stylesheet" href="/static/myapp/css/app.min.css"> why add clutter to the template with: {% load static %} <link rel="stylesheet" href="{% static 'css/default.min.css' %}" /> unless you are dynamically adding part of the path or version at the end of url. I suspect templates with {% load static %} would be less performant(may be ignorable, but still why?) Please kindly enlighten me, i think there might be a strong reason. -
Djnago password reset on users register email id without enabling less secure app
I need help, I have created user login and signup page in Django where I have also implemented Forgot password feature by sending password reset link on registered Gmail email id by referring Django documentation. It is working fine. My question is: To get this emails I have to allow the less secure apps(which is not recommended by Google) OR I have to do 2 step authentication. But in general scenarios or website we don't do like this. We just put our registered email id and we get the link without doing any changes in settings of Gmail or email account. Also, I am only able to send email which I have configurd in settings.py. I want to send email to any email id which user has registered. How can I achieve this. So when any user forgot their password they just have to give email id and they will get password reset link without any changes into their Gmail account. Please help. Thanks in Advance. -
User is not iterable
BElow is crashing at line specific_form.save() whenever the upload function is called, and from upload the saveartifact function is called. AM using django 2.2 and python 3.6.9 PLease help with this error, i also provided some parts of my models and forms. def saveArtifact(post, artifact_object_type, artifact_form, user): artifact = artifact_form.save() artifact.artifact_object_type = artifact_models[artifact_object_type]["object_type"] artifact.user = user # get the username of the user that uploaded the artifact artifact.file_tags = post.get("file_tags", "") artifact.reviewer = get_next_admin() artifact.status = 'queued' artifact.save() specific_form = artifact_models[artifact_object_type]["model_form"](post, instance=artifact_models[artifact_object_type]["model"](artifact=artifact)) specific_form.save() # user it not iterable return artifact, artifact.reviewer @login_required def upload(request,artifact_object_type): if request.method == "POST": c = {} c.update(csrf(request)) rp = request.POST print("device from request: " + rp['device']) print("other device from request: " + rp['other_device_0']) print("other device from request: " + rp['other_device_1']) print("other device from request: " + rp['other_device_2']) print("other device from request: " + rp['other_device_3']) print('artifact_object_type: ' + str(artifact_object_type)) artifact_form = ArtifactForm(request.POST,request.FILES) specific_form = artifact_models[artifact_object_type]["model_form"](request.POST) request.session['initial_post'] = request.POST request.session['artifact_object_type'] = artifact_object_type if artifact_form.is_valid() and specific_form.is_valid(): if len(request.FILES) > 0: # only if a file was actually uploaded print('***** FILE UPLOAD STUFF *****') print('file name: ' + str(request.FILES.get('stored_file_location'))) file_name = artifact_form.cleaned_data.get('stored_file_location') print('File was uploaded, now we will open file tagging tool...') log( user=request.user, action="UPLOADED_FILE", #obj=artifact_models[artifact_object_type]["model"], extra={"file_name":str(request.FILES.get('stored_file_location'))} ) … -
local server for virtual machines
How can I manage multiple virtual machines on VMware via a local web server? That is, to send some console commands to each virtual machine via the server, as well as to receive information from consoles from virtual machines to the server? I plan to make a web server using Django -
One Django app instance for multiple domains
I try to create an Django app which will work for multiple domains with single app instance. For example: there are three domains: group1.com, group2.com, group3.com each domain has restricted content available after login user1 is associated with group1.com and group2.com when user1 log in to group1.com and try to enter group2.com, he will be automatically log in into the group2.com there is URL, e.g. DOMAIN_NAME/posts/ which will show all content for user which is logged in from all domains which are associated with this user (in this case, for user1 there should be all "posts" from group1.com and group2.com) when user1 enter group3.com, he's not logged in I used Django Site framework to associate user with domains - content restriction for user in specific domains works fine. Additionally, I used SESSION_COOKIE_DOMAIN parameter in settings.py for "share" cookie between domains and, unfortunately, it only works for subdomains. For example, after set: SESSION_COOKIE_DOMAIN = '.group.com' and after I wrote simple middleware, I'm able to meet the requirements that I wrote above but only for subdomains, like 'one.group.com', 'two.group.com', 'three.group.com'. I was looking for solution for handle that, but I haven't found an answer for newest Django 3.x framework. Is there any … -
saving model sets None for fields for some mysterious reason
I'm getting an object match from the database and set it's fields asian_1,asian_2,ahline. I clearly see that those three variables aren't None (the fields are None). But still, the object never changes those attributes to the variable values. I call there one method which, when is commented, everything works ok. But the problem is that this method shouldn't cause such problems. This is a part of the code: asian_1,asian_2,ahline = Decimal(asian_1),Decimal(asian_2),Decimal(ahline) # Not none decimal numbers if any([asian_1,asian_2,ahline]): self.logger.info('saving') match = Match.objects.get(pk=response.meta['id']) match.asian_1 = asian_1 match.asian_2 = asian_2 match.ahline = ahline match.save() And Match.save: def save(self, **kwargs): self._convert_nans_to_nones() super().save(**kwargs) def _convert_nans_to_nones(self): for field in self._meta.get_fields(): val = getattr(self, field.name, None) if isinstance(val, decimal.Decimal): if val.is_nan(): setattr(self, field.name, None) # debugger never reaches this line!!!! The _convert_nans_to_nones method just checks all fields before saving just to be sure there will be no Decimal('NAN') values. When I comment this method, everything is saved properly. But when this method is called, all values (asian_1...) are None. BUT! I think that this line is never reached (using debugger and logging) setattr(self, field.name, None) I really can't figure out what is happening here. Do you have any idea? -
Inner join subquery Django ORM equivalent
I have three related tables: Modules +-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | module_id | int(11) | NO | PRI | NULL | | +-------------+-------------+------+-----+---------+-------+ Events +------------------+--------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------------------+------+-----+---------+----------------+ | event_id | int(11) | NO | PRI | NULL | auto_increment | | event_time | datetime(4) | NO | | NULL | | | module_id | int(11) | NO | MUL | NULL | | | file_id | int(11) | YES | MUL | NULL | | +------------------+--------------------------+------+-----+---------+----------------+ Files +--------------+-----------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-----------------------------+------+-----+---------+----------------+ | file_id | int(11) | NO | PRI | NULL | auto_increment | | path | varchar(512) | NO | UNI | NULL | | +--------------+-----------------------------+------+-----+---------+----------------+ So, there are Modules, Events and Files. (Unused fields are trimmed from the tables to simplify). Target: I want to get the latest event that happened on each module and it's file path. What I tried: So, for this, at first I created a naive implementation on Django using subquery: last_event_subquery = Event.objects.filter( module_id__module_id=OuterRef('module__id') ).order_by('-event_time', '-event_id') modules = Module.objects.all().annotate( … -
How to create generic SimpleListFilter that is applied on all list_filter fields in Django?
I have several fields in list_filter. I have to create a generic model.SimpleListFilter that is called for every value in list_filter. I cannot create individual class for every list_filter. class MyModelAdmin(admin.ModelAdmin): list_display = ('client_name','client_application_number') list_filter = ('client_name', 'case_received_date', MyCustomFilter) class MyCustomFilter(admin.SimpleListFilter): title = _('Some Title') parameter_name = 'client_name' def lookups(self, request, model_admin): qs = model_admin.get_queryset(request) # Code def queryset(self, request, queryset): # Apply the filter selected, if any id_val = self.value() if id_val: return queryset.filter(client_name=id_val) -
Deploy Django API on Elastic Beanstalk and have Vue project on Amazon S3
I have a Vue/Django project where Vue manages all frontend code including routing with Vue Router and the Django project is only used as an API with a Sqlite DB. I want to go about deploying this project with AWS and I'v deployed a Vue project on S3 but without a backend. I've also found out how to deploy a django app via Elastic Beanstalk, but is it possible and does it makes sense to have the Django API on Beanstalk, the Vue project on S3, and then have them connected somehow or just use the Beanstalk endpoint. I don't know what this means for the Sqlite DB but this is a small project. Any thoughts? -
Custom "html_email_template_name" not working
I am trying to customise the password reset email sent by the application without success. My application ignores my declaration and keeps sending this one: django/contrib/admin/templates/registration/password_reset_email.html. This is my relevant url.py declaration: # Password reset path('password_reset/', auth_views.PasswordResetView.as_view(template_name = 'account/password_reset_form.html', html_email_template_name='account/password_reset_email.html', success_url = reverse_lazy('account:password_reset_done')), name = 'password_reset'), The file templates/account/password_reset_email.html exists and has contents. Many thanks, I am going nuts over this. -
How to show how many database hits my view makes
I have a queryset that I use in my view and I want to know how many times it's hitting the database. Is there a way to show every hit or count them? Or do I need to know the inner workings of every generic Class Based View to estimate the sum? -
Continue process after 502 Bad Gateway (timeout) error in Nginx/Gunicorn/Django website
I have a website using Django, Gunicorn and Nginx. I use some integrated services (like Mautic) using API requests. But eventually this services stops and the requests throws 502 Bad Gateway error due to a timeout. I searched extensively for answers but found only answers that suggested increasing the server's timeout tolerance time. The problem is that a timeout is inevitable if the services I use go down. The requests are made in Django but the process is interrupted when the error is generated, which means that I cannot handle this exception in Python. I need a way to continue the flow in Django and get a response for the request in Python. -
Docker how do I retain the remote IP address within a container?
As the title states - How do I retain the remote IP address in a container? For example I am running a stock standard container running Python & Django. When I view the logs its displaying the containers IP rather than the remote IP. Its therefor not possible for me to whitelist IP's in my app as every request is from the containers IP. Please could someone point me in the right direction here. -
Pass remote username in Django to a MSSQL Stored Procedure as a parameter
I need the remote username (Windows login) in Django. I I have created a simple Django website which outputs the result of executing a Stored Procedure. Is there anyway I can capture the remote username of the user accessing the website and pass that as parameter to the Stored Procedure before it is executed? P.S be gentle I am new to coding and have been learning and playing with Python Django on the fly. Below is the code I have for executing the Stored Procedure. def sql_results(request, query): sql_query = { 'results': 'SET NOCOUNT ON; EXEC [StoredProcedureName]', } sql = sql_query.get(query, None) with connection.cursor() as cursor: cursor.execute(sql) desc = [ name[0] for name in cursor.description] data = cursor.fetchall() return render(request, 'results.html', {'data': data, 'desc': desc}) -
Django static images arent displayed
I have deployed one of my project with free version in 'pythonanywhere', but the static images arent visible in there. While testing in local everything seems to be working but on deploying and adding static images to it, the images arent visible there. Please help me what went wrong here. In my django templates i used these tags to show images <div class="card-image"> <img src="{{cat.category_image.get_photo_url }}"> </div> Or provide me some documentation links to have a look into developement issues. -
BeatifulSoup How To Scrape List Objects
I am trying to scrape all the list objects from an eBay page into a list called 'listings' using BeautifulSoup in Django and print the length of the list. The eBay list objects I want to scrape are all the ones with class='sresult lvresult clearfix li' however, my code is not working correctly as my 'listings' list is empty. Here are the eBay list objects I am trying to scrape: My code: url = 'https://www.ebay.co.uk/sch/i.html?_from=R40&LH_Complete=1&LH_Sold=1&_nkw=rolex%20submariner&_dcat=31387&rt=nc&LH_ItemCondition=3000&_trksid=p2045573.m1684' content = session.get(url, verify=False).content soup = BeautifulSoup(content, 'html.parser') listings = soup.find_all('li', attrs={'class': 'sresult lvresult clearfix li'}) print(len(listings)) -
Retrieve a flat Queryset of prefetch_related objects from a parent queryset
If I have models like so: class A(models.Model): ... class B(models.Model): a = models.ForeignKey(A) class C(models.Model): b = models..ForeignKey(B) I can get the full queryset: qs = A.objects.all().prefetch_related('b_set', 'b_set__c_set') >> <QuerySet [<A: A object (1)>, <A: A object (2)>, ... ]> What I want to know is there a way to get all the C objects in a flattened queryset, e.g: qs['b_set__c_set'] >> <QuerySet [<C: C object (1)>, <C: C object (2)>, ... ]>