Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Make checks in backend before loading completely new page
Imagine u're on web page and clicking the button. Then from JS through ajax, for example: # .js file $.get(url, data) .done((data) => { window.location.href = data['url'] # loading new page }) .fail((data) => { alert(data['error']); # error message in alert window }) u reffer to some def: # views.py def course(request, id): ... # collecting some data from DB and also making checks course = Course.objects.get(id=id) # What if Course.DoesntExist return JsonResponse({'url': 'path tothis def again but not as ajax_request'}) # OR return render(request, 'page.html', { ... }) BUT! There's some thinkings about it: So before I wanted to return render I'd like to make some checks (when getting necessary objects from DB) and in case of errors (Exception) catch it and show as alert message without getting to the new page. But if all is OK I am as usual loading new page. But that means def has to be able to return render and JsonResponse responses, work not only with ajax_request but with usual request. How can I make it? -
Django form doesn't send POST request and site freezes
I'm trying to set up a basic forum site for my coworkers to use. The problem is that when I attempt to log in to the site, no POST request is sent (shown by the terminal) and the entire website becomes unresponsive. In fact, it's sitting in another browser tab buffering infinitely as I type this. I followed Corey Schafer's Django tutorials to lay the groundwork of the site, as I've never done anything with Django. I finished the tutorial series, and the site was fully functioning (minus the password-reset emails). After finishing the tutorials, I added a tagging feature to the post-form through django-taggit. I had to wait until the head of IT could input some files into the company site to access emails (through SendGrid), but I finally was able to send a single password-reset email to myself. I successfully reset my password, and this is where things started to go wrong. I wanted to change my password back, so I went through the process again, but this time when I pressed the "Send Email" button on the password-reset page, the buffering icon (on Chrome), started going backwards and wouldn't stop. I didn't check my console when this … -
Squashing legacy django appilcation that has a lot of data migrations
We have a legacy application with a lot of different business flows and data migrations. Out migrations have got to a point where migrations run very slow. We haven't quite squashed the migration in a decade We are looking at an approach where we have data migrations in a different app. And then squash the existing migrations -
about reverse url for category get_absolute_url
this is my Category get_absolute_ur def get_absolute_url(self): return reverse('products:categories_display', args=[self.id, self.slug]) This is my product view class category_slug_view(ObjectViewedMixin, DetailView): queryset = Category.objects.all() template_name = "products/product_page.html" This is my product url url(r'^(?P<id>\d+)/(?P<slug>[-\w]+)/$', category_slug_view.as_view()), -
using analytics in django by installing it's package
I am new to django and I'm trying to find a method to count the views of my website, so I encountered a package which name is analytic and i installed it with pip. first I need some help to introduce me what is the meaning and functionality of analytics and then help me to work with that package which i had installed it from pip. thanks in advance -
Factory boy's post_generation hook is not setting attribute on the model object after upgrading Django to 2.2
Consider the case of following two factories. Some part of the code might not make sense as I've tried to anonymise classes and fields. class FactoryA(factory.django.DjangoModelFactory): class Meta: model = ModelA name = 'John' @factory.post_generation def post_hook(self, create, extracted, **kwargs): post_generated_objs = [] for i in range(3): post_generated_objs.append(SomeOtherFactory(id=i+100)) self.post_generated_objs = post_generated_objs return post_generated_objs class FactoryB(factory.django.DjangoModelFactory): class Meta: model = ModelB some_data = factory.LazyAttribute(lambda obj: obj.model_a.post_generated_objects[0]) It used to work as expected before upgrading Django to latest version, i.e. 2.2. I am using latest version of factoryboy as well. The issue here is that factoryboy is not setting the attribute post_generated_objs to the returned object of ModelA created by FactoryA. The error I get is AttributeError: 'ModelA' object has no attribute 'post_generated_objs' Python 3.7 django==2.2.4 factory-boy==2.12.0 pytest==4.4.0 pytest-django>=3.5 pytest-xdist==1.28.0 -
Creat User by Api in django with rest framework !!1
some problems with creating user in Django!!!! Hello guys H want to create user by Api in Django and i confused how to do that. the first thing is should i logged in with superuser account and then create new user(or can i create anonymously? is that secure? ) The other questions is how to Authenticate anonymous user if anonymous person can create user. -
How to test postgres statement_timeout in django
I have added both connect and statement timeouts for a postgres database in my django service. So the relevant django setting looks like; _CONN_TIMEOUT = 5 _STATEMENT_TIMEOUT = 3000 # millisecond DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": "mobile_loans", # ..., "OPTIONS": { "connect_timeout": _CONN_TIMEOUT, "options": "-c statement_timeout={0}ms".format(_STATEMENT_TIMEOUT), }, } } And then, I'm writing tests like this; class DbTimeoutTest(TestCase): def test_db_statement_timeout(self): """ test carrying out an SQL query that takes longer than the configured postgres `statement_timeout` value """ # set statement_timeout to 1 millisecond mock_s_timeout = 1 with self.settings(_CONN_TIMEOUT=5, _STATEMENT_TIMEOUT=mock_s_timeout): self.assertEqual( settings.DATABASES["default"]["OPTIONS"]["options"], "-c statement_timeout={0}ms".format(mock_s_timeout), ) Book.objects.create(name="Hello") However, that test is not working. - The assert self.assertEqual does not pass, meaning that the setting override did not work. - I would expect the Book.objects.create statement to fail with timeout but it does not. So questions; 1. How do I test for postgres statement_timeout?(been able to also test for connect timeout would also be a plus) 2. How do I catch, the statement timeout error in my code? -
Heroku CI: run django tests with postgis
I have a django project with postgis and I want to run tests with Heroku CI. I want to use heroku-postgresql:in-dyno addon's plan as it recommended. But seems like in-dyno plan doesn't support postgis extension. Is it possible to setup the postgis extension manually? -
How to have a writable file ready on google cloud bucket for using with library save functions
So I am using moviepy library to get a gif out of a video file that is located on my google cloud bucket. now I am using google-cloud-storage package for the save & read operations so far. But I was using the package methods. Now moviepy requires a filepath as a parameter to run it's write_gif method to produce the gif. After a while I figured I can use gsutil sort of solution with it. I came across gcsfs. But problem is I can't figure out how to have the file writable for write_gif to work since even here I am able to open the blank bucket gif file using open method on gcsfs object vfile = 'https://storage.googleapis.com/test.appspot.com/dir/sample.mp4' tfile = 'https://storage.googleapis.com/test.appspot.com/dir/sample.gif' #non-existant-file clip = VideoFileClip(vfile) gifClip = clip.subclip(8, 10) target_gc_thumb = bucket.blob('dir/sample.gif') target_gc_thumb.upload_from_string('',content_type='image/gif') #blank-gif-file-saved fs = gcsfs.GCSFileSystem(project='test') with fs.open('test.appspot.com/dir/sample.gif', 'w', metadata={"Content-Type":"image/gif"}) as f: gifClip.write_gif(f,fps=5) #raises-error-since-it-expects-a-local-file-path clip.close() I get this error since I not passig a string that represents the local file path : Could not find a format to write the specified file in mode '?' -
Django Model QuerySet: RuntimeError generator raised StopIteration
I've recently updated my Django to version 2.2.4 and python to 3.7, and I encounter this error randomly: Traceback (most recent call last): File "/foo/venv/lib/python3.7/site-packages/django/db/models/query.py", line 73, in __iter__ obj = model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end]) File "/foo/venv/lib/python3.7/site-packages/django/db/models/base.py", line 511, in from_db for f in cls._meta.concrete_fields File "/foo/venv/lib/python3.7/site-packages/django/db/models/base.py", line 511, in <listcomp> for f in cls._meta.concrete_fields StopIteration I tried to debug the code to find out what's happening, and it seems the from_db function in django.db.models.base.py is causing this error: # /foo/venv/lib/python3.7/site-packages/django/db/models/base.py ... @classmethod def from_db(cls, db, field_names, values): if len(values) != len(cls._meta.concrete_fields): values_iter = iter(values) values = [ next(values_iter) if f.attname in field_names else DEFERRED for f in cls._meta.concrete_fields ] new = cls(*values) new._state.adding = False new._state.db = db return new ... next(values_iter) is raising this error and it seems that Django devs should surround that with try except block to make this work in python 3.7, but my question is, is there a way to overcome this issue as a temp fix or not? thanks. -
How to say python scheduler to run a thread after finishing a function in django?
I want to call a function inside one of my views in another thread to avoid blocking the user request. But when I tried it using threading.Thread I got the same result as running single thread. Can anybody help me with this problem? def my_view(request): print() threading.Thread(target=foo) return HttpResponse('ok') def foo(): # blocks for 10 seconds -
Django models.py year field
I created a database with population and years by IntegerField but is there any way to create the year field by datefield just for years neither months or days? class Nufus_ilce(models.Model): city = models.CharField(max_length=15) year = models.IntegerField(default=0) population = models.IntegerField(default=0) def __str__(self): return '%s %s' % (self.city, self.year) -
Get Image Field Absolute Path in Django Rest Framework
I've a periodic celery task which needs to store representation of a object in a specific json field. Here is the simplified model structure. Parent <-- ChildWrapper <-- Child Image So basically I've a 'ChildImage' model referring to 'ChildWrapper' which in turn refers to 'Parent'. class Parent(TimeStampedModel): label = models.CharField(max_length=30, unique=True) live_content = JSONField(blank=True, null=True) is_template = models.BooleanField(default=False) reference_image = models.ImageField(upload_to=get_web_row_reference_image_path, blank=True, null=True) # Around 8 Other Fields def __str__(self): return '%s' % self.label class ChildWrapper(TimeStampedModel): name = models.CharField(max_length=25, blank=True, null=True) row = models.ForeignKey(WebRow, on_delete=models.CASCADE, related_name='web_column') order = models.PositiveIntegerField(default=0) # Around 20 Other Fields def __str__(self): return '%s' % self.name class ChildImage(TimeStampedModel): image = models.ImageField(upload_to=get_web_image_path) column = models.ForeignKey(WebColumn, on_delete=models.CASCADE, related_name='web_image') # Around 10 Other Fields def __str__(self): return '%s' % self.column This is the serializers defined for the models. class ChildImageSerializer(serializers.ModelSerializer): class Meta: model = ChildImage fields = '__all__' class ChildWrapperSerializer(serializers.ModelSerializer): web_image = ChildImageSerializer(read_only=True, many=True) class Meta: model = ChildWrapper fields = '__all__' class ParentSerializer(serializers.ModelSerializer): web_column = ChildWrapperSerializer(many=True, read_only=True) class Meta: model = Parent fields = '__all__' Here is the periodic celery task which does the required @app.task(bind=True) def update_data(self): # Get Parent By a condition. parent = Parent.objects.filter(to_update=True).first() parent.live_content = None parent.live_content = ParentSerializer(parent).data print(parent.live_content) parent.save() The above task … -
django CSRF token cookie not set for some users
I have been getting sporadic CSRF errors in an app that is mostly working ok. I do everything as I'm supposed to do: I use {% csrf_token %} in my template for normal forms and in my ajax POSTs I set the X-CSRFToken header: $.ajaxSetup({ beforeSend: function(xhr, settings) { xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken')); }, }); I'm even forcing the cookie to be set in all views by writing a custom Middleware that calls get_token def CSRFForceCookieMiddleware(get_response): def middleware(request): response = get_response(request) get_token(request) # Force to set cookie in all responses return response return middleware Everything works OK in my localhost and in production for most users. But for some users I get 403 CSRF validation error. I added a lot of debug info. Turns out that even if CsrfViewMiddleware is setting the csrftoken is setting the cookie in the response, in the actual browser the cookie is not set ($.cookie('csrftoken') is null). So when the ajax call is made, there is no cookie present in the request. So, I guess this pretty much means that some users' browsers are blocking this cookie? Anyone else had this experience? -
Run pipline in Azure for Django project
Run pipline in Azure for test and get this error enter image description here -
Docker / ptvsd / Django: Failed to attach (connect ECONNREFUSED )
I am trying to implement a debugging plugin ptvsd into my existing dockerized application Running on Google Compute Engine with Ubuntu 18.04. The entire application is containerized with Docker-Compose. Back-end Plugin: Django manage.py: #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import os import sys import ptvsd def main(): os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoserver.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc ptvsd.enable_attach(address=('0.0.0.0', 5050)) execute_from_command_line(sys.argv) if __name__ == '__main__': main() Launch.json { "name": "Attach Remote Django", "type": "python", "request": "attach", "pathMappings": [ "localRoot": "${workspaceRoot}/djangoserver", "remoteRoot": "/usr/src/" ], "port": 5050, "secret": "secret", "host": "localhost" } docker-compose.yml web: build: ./djangoserver command: gunicorn djangoserver.wsgi:application --noreload --nothreading --bind 0.0.0.0:8001 volumes: - ./djangoserver:/usr/src # entrypoint: /entrypoint.sh ports: - 5050:5050 expose: - 8001 env_file: .env.dev depends_on: - db_development_2 stdin_open: true Whenever i a do build and run the docker-composer, it starts without any problems, but later on, when I am trying to connect to the server with the debugger, I receive following error: Failed to attach (connect ECONNREFUSED 127.0.0.1:5050) -
Django search form for filtering
I am currently doing a search using forms This is my views.py class HomeView(generic.ListView): model = Consultant template_name = 'sogeti/home.html' def get_queryset(self): query = self.request.GET.get('q') if query: return Consultant.objects.filter( Q(first_name__icontains=query) | Q(last_name__icontains=query) | Q(group__title_techGroup__contains=query) | Q(practices__title_practice__contains=query) ) else: return Consultant.objects.all() and this is my home.html <form action="" method="get" class="form-inline"> <input type="text" name="q" placeholder="Enter Keyword" value="{{ request.GET.q }}" class="form-control"> <select name="filter" class="form-control"> <option value="all">All</option> <option value="people">People</option> <option value="certification">Certification</option> <option value="skillset">Skillset</option> </select> <input type="submit" value="Search" class="btn btn-default"> </form> My first problem is that when it tries to search something (Eg: bla) that is not in my database, it returns a blank screen. Nothing at all. Tried searching but could not get any answers. My second problem is how am I able to specify my search using HTML select and options to filter. As you can see from my home.html I have the tag with option value but no idea how to utilize it for Django. Thank you so much for your help! Really appriciate it. -
Using the 'get' form to pass the value to the next view in Django
I try to pass the value (from the completed form) form one view to the next view using the get method. My view looks like this. def search(request): if request.method == 'GET' and 'search' in request.GET: form = SearchForm(request.GET) if form.is_valid(): return HttpResponseRedirect(reverse('app:search_results')) else: form = SearchForm() context = {'form': form} return render(request, 'search.html', context) and simple forms.py class SearchForm country = models.ChoiceField(choices=COUNTRY) city= models.ChoiceField(choices=CITY) I tried to use args (to pass city and country to search_results view) HttpResponseRedirect(reverse('app:search_results', args=[country, city])) but my url next views looks like this: http://127.0.0.1:8000/search-results/USA/new-york/ but i expects something like that (after redirecting to the next view): http://127.0.0.1:8000/search-results/?country=USA&city=new-york& How can i do this? For example After choosing the location and type of work on this page, we see a similar url to example 2 not 1 (as on most websites). Any help will be appreciated. -
How to add generate S3 pre-signed URL to drf serializer
I created my own mix to generate url access to AWS files class AwsUrlMixin: def _get_aws_url(self, url): if not settings.AWS_ACCESS_KEY_ID: return url s3_client = boto3.client( 's3', aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY, region_name=settings.AWS_S3_REGION_NAME ) params = {'Bucket': settings.AWS_STORAGE_BUCKET_NAME, 'Key': f'{settings.AWS_STORAGE_LOCATION}/{url}'} return s3_client.generate_presigned_url('get_object', Params=params, ExpiresIn=86400) And successfully added it to the admin panel @admin.register(Documents) class EvaluationAdmin(AwsUrlMixin, admin.ModelAdmin): icon = '<i class="material-icons">attachment</i>' form = DocumentsForm list_display = ('filename', 'type', 'view_file') list_display_links = ('filename',) def view_file(self, obj): return mark_safe(f'<a href="{self._get_aws_url(obj.datafile)}">{obj.datafile}</a>') But I'm having trouble adding it to the API. How do I add a new field to the logic? Use serilaizer? I try this way class DocumentsSerializer(AwsUrlMixin, serializers.ModelSerializer): class Meta: model = Documents fields = ('id', 'filename', 'datafile', 'type', 'created', '_view_file') def _view_file(self, obj): return mark_safe(f'<a href="{self._get_aws_url(obj.datafile)}">{obj.datafile}</a>') But got error django.core.exceptions.ImproperlyConfigured: Field name `_view_file` is not valid for model `Documents`. -
How to eliminate duplicate rows in django model admin with ManyToMany fields
It's known issue that Django duplicates some of the entries of models with many-to-many relation. In typical situation we could use distinct method to avoid duplicates, but what to do in model admin's search_fields? Should i override get_queryset method or there are better approaches? How would you do it? -
Unique database entry determined by attributes
I have the following class: from django.db import models class Person(models.Model): name = models.CharField(max_length=200) age = models.CharField(max_length=200) and I make two identical instances of this class: alex_1 = Person(name='Alex', age=30) alex_1.save() alex_2 = Person(name='Alex', age=30) alex_2.save() This will save 2 entries in the person database. Is there any way to prevent the second instance (alex_2) from being saved as it's technically a duplicate? i.e., can you implement the __eq__ and __hash__ functions for models.Model instances? -
Sent data through POST method to show in AJAX request
Ajax method to post and passing a string to the views.py, the post method in the views.py can receive values from ajax but I cannot get the results value to print back in the ajax success method. I have tried to return HTTPresponse, redirect, render but nothing seem to work for me. //Ajax// $("#viewData").click(function(event){ $.ajax({ type: "POST", data: { tempData : "permView", csrfmiddlewaretoken: '{{ csrf_token }}', }, success: function(result) { console.log('{{ result }}') }, }); event.preventDefault() }); }); //Python//views.py class SpreadSheetView(TemplateView): template_name = 'spreadsheet.html' def get(self, request, *args, **kwargs): return render(request, self.template_name, {'type': '86'}) def post(self, request): if request.POST['tempData'] == 'permView': return render(request, self.template_name, {'result': "test result"}) -
How can i see the request data of a django form submit in Chrome?
I want to see the request payload but all i see is the request and response headers in the dev tools network tab. A similar question is asked here but with no answer. How can I see submitted POST request data in Chrome DevTools for multipart/form-data Can someone please tell me how i can see the request and response payload of a django form submit? thank you -
How can I 'bound' a modelformset to an object, fill it and editing in the same page?
I m new in Django and development. I have a items list, and when I clik on the result button of a test, I would like to have a modelformset for this specific test where i can fill it and editing in the same page. The code I wrote is 'working' but the problem it's when I fill the form and submit it, it' s editing the same for all the test. I hope you will understand my explanation( not english speaker) Here my code: model.py: class ResultatTest(models.Model): id = models.AutoField(primary_key=True) test = models.ForeignKey('Test', on_delete=models.CASCADE) services = models.CharField(max_length=30, choices=SERV_CHOICES) notes = models.IntegerField(null=True) comments = models.TextField() def __str__(self): return self.services view.py: def resultatTest(request, id): id = get_object_or_404(Test, pk=id) ResultatTestFormset = modelformset_factory(ResultatTest, fields=('test', 'services', 'notes', 'comments'),extra=1) if request.method == "POST": formset = ResultatTestFormset(request.POST)#queryset=form.object.filter(test=test_id) if formset.is_valid(): instances = formset.save(commit= False) for instance in instances: instance.save() else: formset = ResultatTestFormset() return render(request, "Portail/Details/resultat.html", {'id': id,'formset': formset}) items/test.html: onclick ="window.location ='/resultat/{{test.id}}','_self'; url.py: path('resultat/<int:id>/',resultatTest,name='resultat'), resultat.html: <form action="" method="post" id="resultatForm "> {% csrf_token %} <table id='supertable' border=1> {{ formset.management_form }} {% for form in formset %} {% if forloop.first %} <tr> {% for field in form.visible_fields %} <td style="background: cyan; line-height:1; white-space:nowrap;width:500px;" >{{ field.label_tag }}</td>< {% endfor …