Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
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 … -
OperationalError - cursor "_django_curs_<id>" does not exist for edit and add form
On accessing a url to add or edit form, which would render a template to edit the form gives the error - OperationalError at /edf/data_asset_format_edit// I tried running makemigrations and migrate, even after that i get the above error. I tried creating a new object for the same model from shell (python manage.py shell), it works. OperationalError at /edf/data_asset_format_edit/466/ cursor "_django_curs_140026377369344_10" does not exist Request Method: GET Request URL: https:///edf/data_asset_format_edit/466/ Django Version: 1.11 Exception Type: OperationalError Exception Value: cursor "_django_curs_140026377369344_10" does not exist Exception Location: /opt/edfprod/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py in execute_sql, line 880 Python Executable: /opt/edfprod/python/bin/python Python Version: 2.7.11 Traceback: File "/opt/edfprod/python/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner 41. response = get_response(request) File "/opt/edfprod/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request) File "/opt/edfprod/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/opt/edftest/test2/ui/dna_django/edf/views.py" in data_asset_format_edit 726. return render(request, 'edf/data_asset_format_edit.html', {'form': form}) File "/opt/edfprod/python/lib/python2.7/site-packages/django/shortcuts.py" in render 30. content = loader.render_to_string(template_name, context, request, using=using) File "/opt/edfprod/python/lib/python2.7/site-packages/django/template/loader.py" in render_to_string 68. return template.render(context, request) Views - def data_asset_format_edit(request, edf_data_asset_format_id): ... ... pa = EdfProviderDataAssetVw.objects.get(p_data_asset_id=data_asset_format.data_asset_id) form = EdfDataAssetFormatForm(instance=data_asset_format, data_asset=daf_data_asset, initial={'provider_name': pa.provider_name, 'provider_id': pa.provider_id, 'p_data_asset_name': pa.p_data_asset_name, 'p_data_asset_id': pa.p_data_asset_id, 'updated_by': User.objects.get(username=request.user)}) return render(request, 'edf/data_asset_format_edit.html', {'form': form}) I dont understand what is causing this error. -
How to perform calculation for a model field and display the value in the template?
I have a model called 'Candidate', which has the fields Experience,New Experience,Date_posted. I'm using CreateView form to store the values in the database. When I enter experience and date_posted(set to timezone.now) for the first time, the 'new experience' value should also be set to 'experience' value by default. Now this 'new experience' value should get incremented after every month. For example, experience=2.4 ( 2 years 4 months ), new experience =2.4( set automatically ), So, If I open my template(website page) 1 month from now, the 'experience' and 'date_posted' values must be same, but 'new experience' = 2.5 ( 2 years, 5 months ) Also, I want to use the format, 2.12 ( for 2 years 12 months )and 3.1 ( for 2 years 13 months ) How do I achieve this? -
500 internal error in django slug with unicode in production
I have a problem with str path converter. I have unicodes in blog's article slug. On local host everything works well but I'm getting 500 Internal Error on production. When I change slugs to ASCII characters it works fine and article page loads with no error. models.py: class Post(models.Model): ... slug = models.SlugField( max_length=128, unique=True, allow_unicode=True, ) ... urls.py: urlpatterns = [ ... path('blog/<str:slug>/', views.PostDetailView.as_view(), name='post_detail'), ] views.py: class PostDetailView(DetailView): model = Post template_name = 'post_detail.html' This is the ERROR: Internal Error The server encountered an unexpected condition which prevented it from fulfilling the request. thanks -
importing current_app from celery is gives ValueError: attempted relative import beyond top-level package
Yes I know there are lot's of similar questions on stack-overflow related to this value-error and I tried all the solutions from them but as I am new to Django and python I am not able to solve this issue. I have one project called my_backend which have the following file structure. main_project/ cmb_backend/ __init__.py celery.py urls.py second_app/ __init__.py moduleZ.py my_env/ bin/ include/ lib/ python 3.7/ site-packages/ celery/ django_celery_beat admin.py I have used celery for the periodic task so I have added one celery.py file in my main application my_backend. I have also installed django_celery_beat using pip and inside that, they have imported celery using below code. # admin.py file in the django_celery_beat lib from celery import current_app from celery.utils import cached_property so when I run this command python3 my_backend/setup_database.py it is giving me an error like ImportError: cannot import name 'current_app' from 'celery' (/Users/pankaj/Desktop/Pankaj/MyJangoProjects/My_Project/my_backend/celery.py) so from this error, I found that when I am running above command admin.py is importing current_app from celery but it is looking in the wrong file so to solve this error I am using relative import and adding .. in front of import statement but still, it's not working # admin.py file in the … -
How to get multiple files from a django FileField after model form object has been saved
I have a django ModelForm with an additional field to upload some files. However, I need the saved model before I can do anything with the files, and I'm not sure where or how to do that. I'm following the docs here. I either need to get the saved model in the FormView or I need to handle it in the Form: class MessageForm(forms.ModelForm): class Meta: model = Message file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True})) def save(self, *args, **kwargs): super().save(*args, **kwargs) files = self.cleaned_data.get('files') # do stuff with files here... # BUT I only get ONE file here, regardless of how many are uploaded with the form. Is there a way for me to get all the files in the Form's save method? Or, I can get all the files through the view, but how can I get the saved model that I need? This form is creating a new object, and I need that object before I can do stuff with the files: class FileFieldView(FormView): form_class = MessageForm template_name = 'upload.html' # Replace with your template. success_url = '...' # Replace with your URL or reverse(). def post(self, request, *args, **kwargs): form_class = self.get_form_class() form = self.get_form(form_class) files = request.FILES.getlist('file_field') if … -
Python : How to determine the maximum threads a Django Project can spawn?
I have a Django application, in which I have implemented kafka to process some orders. Each order is associated with an offering. Now for each offering I create a topic on Kafka and assigns a new consumer to listen to that topic in a new thread. However after a certain extent let's say about 100 offering, my program is not able to spawn a new thread. RuntimeError: can't start new thread # Consumer Assignment @shared_task def init_kafka_consumer(topic): try: if topic is None: raise Exception("Topic is none, unable to initialize kafka consumer") logger.info("Spawning new task to subscribe to topic") params = [] params.append(topic) background_thread = Thread(target=sunscribe_consumer, args=params) background_thread.start() except Exception : logger.exception("An exception occurred while reading message from kafka") def sunscribe_consumer(topic) : try: if topic is None: raise Exception("Topic is none, unable to initialize kafka consumer") conf = {'bootstrap.servers': "localhost:9092", 'group.id': 'test', 'session.timeout.ms': 6000, 'auto.offset.reset': 'earliest'} c = Consumer(conf) logger.info("Subscribing consumer to topic "+str(topic[0])) c.subscribe(topic) # Read messages from Kafka try: while True: msg = c.poll(timeout=1.0) if msg is None: continue if msg.error(): raise KafkaException(msg.error()) else: try: objs = serializers.deserialize("json", msg.value()) for obj in objs: order = obj.object #Fix temporary (2006, 'MySQL server has gone away') from django.db import close_old_connections close_old_connections() … -
Can I use React with Django Framework simultaneously?
I'm learning React at the moment, and I was just wondering if I can use React with Django framework together down the line. Or, what are the recommendations/technology to use with React? -
django datepicker on datatable not working
I want to filter my datatable between two date range. I tried many codes for datepicker but nothing worked for me. When I inspected my console, I found it saying "$.fn.dataTable is undefined". Overall my datepicker is not woking in django. Please help as I'm new to javascipt. I have taken this js code from somewhere. <script> /* Custom filtering function which will search data in column four between two values */ $(document).ready(function () { $.fn.dataTable.ext.search.push( function (settings, data, dataIndex) { var min = $('#min').datepicker("getDate"); var max = $('#max').datepicker("getDate"); var startDate = new Date(data[3]) if (min == null && max == null) { return true; } if (min == null && startDate <= max) { return true;} if(max == null && startDate >= min) {return true;} if (startDate <= max && startDate >= min) { return true; } return false; } ); $("#min").datepicker({ onSelect: function () { table.draw(); }, changeMonth: true, changeYear: true , dateFormat:"m/d/y"}); $("#max").datepicker({ onSelect: function () { table.draw(); }, changeMonth: true, changeYear: true, dateFormat:"m/d/y" }); var table = $('#example').DataTable(); // Event listener to the two range filtering inputs to redraw on input $('#min, #max').change(function () { table.draw(); }); }); </script> <table border="0" cellspacing="5" cellpadding="5"> <tbody> <tr> <td>Minimum … -
How can we get data from multiple data base in django orm?
I want to get data from multiple databases in Django ORM Select em.EMPLOYEEID_N, tran.TRANSFERID_N,dep.HRDEPT_ID_V as HRDEPT_ID_V, dep.ACCOUNTSDEPT_ID_V as ACCOUNTSDEPT_ID_V, dep.ADMINDEPT_ID_V as ADMINDEPT_ID_V, dep.ITDEPT_ID_V as ITDEPT_ID_V,NVL(chk.HR_DPT,'N') HR_DPT, NVL(chk.ACCOUNTS_DPT,'N') ACCOUNTS_DPT , NVL(chk.ADMIN_DPT,'N') ADMIN_DPT , NVL(chk.IT_DPT,'N') IT_DPT, dep.RECIEVINGACCOUNTSDEPT_ID_V, NVL(dep.RECIEVINGACCOUNTSCHECK,'N') RECACCOUNTS_DPT, hr.UNITID_N USRUNITID,DEP.UNITID_N TFRUNITID From PYR_EMM_EMPLOYEEMASTER em,PYR_EMM_TRANSFERINOUTSTATUS tran,PYR_EMM_TRANSFERADV_CHECKLIST chk,PYR_EMM_TA_DEPTALERT dep, PYR_EMM_EMPLOYEEMASTER hr Where em.EMPLOYEEID_N = tran.EMPLOYEEID_N and tran.TRANSFERID_N = chk.TRANSFERID_N and tran.EMPLOYEEID_N = chk.EMPLOYEEID_N and tran.TRANSFERID_N = dep.TRANSFERID_N and tran.EMPLOYEEID_N = dep.EMPLOYEEID_N and hr.ACTDIRUSERNAME_V = 'ithd.ggn' and chk.UNITID_N = dep.UNITID_N and (dep.HRDEPT_ID_V = 'ithd.ggn'||'@bilt.com' or dep.ACCOUNTSDEPT_ID_V = 'ithd.ggn'||'@bilt.com' or dep.ADMINDEPT_ID_V = 'ithd.ggn'||'@bilt.com' or dep.ITDEPT_ID_V = 'ithd.ggn'||'@bilt.com' or dep.RECIEVINGACCOUNTSDEPT_ID_V = 'ithd.ggn'||'@bilt.com') -
Is it reliable to use oauth and rest framework in main web application?
I just started designing the new web application , in my old design i used sessions, web page rendered in back end, now we thought to design using REST and oauth , normally i found that oauth and REST and using to integrate with third party services/application, so my question is can i do it in my main web front end in building in angular, and api services in rest and oauth? , is it reliable ? -
Unable to compare xls data from a cell with unicode string
I am importing a .xls file and want to perform some checks on data written in specific cells. I did this: wb = xlrd.open_workbook('foobar.xls') sheet = wb.sheet_by_index(0) if sheet.cell_value(0, 3) != u'special' or sheet.cell_value(0, 3) != u'Special': error_msg = 'The fourth column head should say "special"' This throws error all the time even if the cell does say 'special' I even did print(sheet.cell_value(0, 3)) to double check. And type(sheet.cell_value(0, 3)) shows its unicode, which is why im doing u'special'. Why is the if statement always true? please help.