Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django - Handling several file upload requests at a time?
I am developing a django application which handles lots of file uploads from multiple clients periodically, Each file is around 1 to 10 megabytes, Since uploads are thread blocking i can only serve no:of requests equivalent to the no:of uwsgi workers/processes (4 here). What should i do to increase throughput? is it advisable to increase no:of processes/workers in uwsgi? what should be the upper limit? or is there any other solution that i can use in this scenario ? Stack: django+uwsgi+nginx running on amazon ec2 and s3 buckets used for storing zip files. -
django oauth request with bearer token returns "relative imports require the 'package' argument" error
I am trying to implement the django oauth toolkit by following this tutorial: https://django-oauth-toolkit.readthedocs.io/en/latest/tutorial/tutorial_03.html But I can't get past this step as the curl get request at the bottom of the page returns the error relative imports require the 'package' argument I've completed the first part of the tutorial exactly as instructed and it is all working fine up to this point. I'm using django 1.8. Has anyone else encountered this problem? -
Django - How to refer to a model's primary key
Model: (notice kundenr set as primary key) (means customer number in Norwegian) class Kunde(models.Model): avd = [('610','610'), ('615', '615'), ('620', '620'), ('625', '625'), ('630', '630'),('635', '635'),('640', '640'),('645', '645'), ('650', '650'), ('655', '655')] avdeling = models.CharField(max_length=3, choices=avd) selskap = models.CharField(max_length=50, unique=True) orgnr = models.CharField('Organisasjonsnummer', max_length=11) kundenr = models.CharField('Kundenummer', max_length=15, unique=True, primary_key=True) gatenavn = models.CharField(max_length=50,) postnr = models.CharField('Postnummer', max_length=4) poststed = models.CharField(max_length=30) kommune = models.CharField(max_length=30) kontaktperson = models.CharField(max_length=50) tlf_kontor = models.CharField("Telefon kontor", max_length=12) tlf_mobil = models.CharField("Telefon mobil", max_length=12) epostadresse = models.EmailField() timestamp = models.DateField(auto_now_add=True) def get_absolute_url(self): return reverse("kunder:detaljer", kwargs={"id": self.kundenr}) def __str__(self): return self.selskap class Meta: ordering = ['selskap'] View def kunde_detail(request, kundenr): template = 'kunder/detaljer.html' instance = get_object_or_404(Kunde, kundenr=kundenr) # pandas code removed as it was quite long utvalgt_kunde = 727636 # this should refer to the chosen Kunde.kundenr # more pandas code removed context = { "utvalgt_kunde": instance.kundenr, "selskap": instance.selskap, "instance": instance, "kunde_script": script, "kunde_div": div } return render(request, template, context) In the views function I need 'utvalgt kunde' = to the 'kundenr' since the graph needs to be dynamic. Here is a screenshot of the list of customers. When I click on a customer name I get directed to the customer details page. Here is a screenshot of my … -
I want to rearrange fields of the same class in django as parent and child
class Category(models.Model): parent =models.ForeignKey('self', null=True, blank=True, related_name='children') user = models.ForeignKey(User, db_index=False) Parent1 user1 user2 user3 (Alphabatical order) parent2: user4 user5 user6 (Alphabatical order) How can you help me? -
django StreamingHttpResponse - catch exception raised in streming_content iterator
I'm trying to handle errors while returning a StreamingHttpResponse: The exception raised manually inside the iterator used as streming_content is not catched. here's the code: def reportgen_iterator(request, object_id): output_format = request.GET.get('output', 'pdf') debug_mode = request.GET.get('debug', False) response_data = { 'progress': 'Retrieving data...', } # code.... yield json.dumps(response_data) # code ... raise Exception('bla bla') # manually raised exception # other code ...... yield json.dumps(response_data) class StreamingView(View): def get(self, request, object_id): """ """ stream = reportgen_iterator(request, object_id) try: response = StreamingHttpResponse( streaming_content=stream, status=200, content_type='application/octet-stream' ) response['Cache-Control'] = 'no-cache' return response except Exception as e: # exception not catched return HttpResponseServerError(e.message) any help about how correctly handle this? The except clause is never reached. Thanks -
Access Python Rest API using AJAX jQuery
I don't know python. I'm just requesting Python REST APIs using jQuery and AJAX. Here's my Code so far: <!DOCTYPE html> <html> <head> <title>Announcements</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <div id="announcements"> </div> <script> $(document).ready(function(){ $("body").bind("ajaxSend",function(elm,xhr,s){ xhr.setRequestHeader('X-CSRF-Token',"d9wILtV77wDDb3qP6Zl9wtSQ6tGJgZbq"); }); $.ajax({ dataType: "json", method: "GET", url: "/mhtsessions/annoucements/", data: { "CSRF": "d9wILtV77wDDb3qP6Zl9wtSQ6tGJgZbq" } }).done(function(data){ $("#announcements").html(data); }); $.getJSON("/mhtsessions/annoucements/",function(data){ $("#announcements").html(data); }); }); </script> </body> </html> I also need to pass CSRF Token to access the APIs. Where CSRF Token is generated by Django Rest Framework. -
Parseing unicode text for SQL statement, python 2.7
I'm using django with psycopg2. I have a form that captures some text and I need to put them into my database. I can insert them correctly into my table when the text is the typical english text. But my users can write the text in multiples languages (spanish, german, etc) so when they write some text that contains for example: ', ï, ñ, etc. my query can't be executed. The problem that i'm seeing is that in Python 2.7 there are no str, the text comes in unicode format (u'text') so when I try to put 'español' I get 'espa\xc3\xb1ol'. I can save them into my table, but I need the original 'español' into my text, not 'espa\xc3\xb1ol'... I've tried the following, researching into stackoverflow and other webpages: Encode/Decode the text, but this doesn't solve it since this writes the text in utf-8 for example, and it's not the original word/text. Use the json.dump function, but it doesn't solve it again since it leaves the u'text' and let's them 'text'. So the problem about 'espa\xc3\xb1ol' keeps remaining. Use str(text) to parse it, but since it's python 2.7, str is for BYTE and gives me an error when it comes … -
ConnectionError: exception while running queryset on elasticsearch
I'm using django-elasticsearch in my django application ,here is the link to follow the steps https://github.com/liberation/django-elasticsearch Before configuration with my django app I need to first test elasticsearch in shell as per mentioned in above link. Here is my model: from django.db import models from django_elasticsearch.models import EsIndexable class Product(EsIndexable, models.Model): product_name = models.CharField(max_length=100) slug = models.SlugField(max_length=100) After this as per mentioned in steps I did this in python shell >>> q = Product.es.search('sample') >>> q and I got this error in shell ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x10828e250>: Failed to establish a new connection: [Errno 61] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x10828e250>: Failed to establish a new connection: [Errno 61] Connection refused) How do I proceed now, so that I should get result as [{'id': 1, 'product_name': 'sample'}, {'id': 2, 'product_name': 'samp'}, ...] Please help thanks in advance. Also please suggest some good documentation and step by step workflow to achieve this functionality in my django app to search the products based on their names. -
Django - How to add data to a ManyToMany field model?
I have the following models, view and template: models.py: class Batch(models.Model): material_id = models.ManyToManyField(AssetMetadata) user = models.ForeignKey(User) def __str__(self): return 'Batch_' + str(self.pk) + '_' + self.user.username class AssetMetadata(models.Model): id = models.DecimalField(6).auto_creation_counter material_id = models.CharField(max_length=256, blank=True) series_title = models.CharField(max_length=256, blank=True) season_title = models.CharField(max_length=256, blank=True) season_number = models.IntegerField(default=0) episode_title = models.CharField(max_length=256, blank=True) episode_number = models.IntegerField(default=0) synopsis = models.TextField(max_length=1024, blank=True) ratings = models.CharField(max_length=256, blank=True) def __str__(self): return self.material_id views.py: def assets_in_repo(request): asset_list = AssetMetadata.objects.order_by('id').all() page = request.GET.get('page', 1) paginator = Paginator(asset_list, 50) try: assets = paginator.page(page) except PageNotAnInteger: assets = paginator.page(1) except EmptyPage: assets = paginator.page(paginator.num_pages) if request.method == 'POST': batch_list = request.POST.getlist('batch') print(batch_list) return render(request, 'assets_db/assets.html', {'assets': assets}) snippet from template: <div class="table-responsive"> <form method="post">{% csrf_token %} <input type="submit" value="Create Batch" align="right"> <table class="table table-striped" id="myTable"> <tr> <th>ID</th> <th>Material ID</th> <th>Series Title</th> <th>Season Tile</th> <th>Season Number</th> <th>Episode Title</th> <th>Episode Number</th> <th>Create Batch</th> </tr> {% for i in assets %} <tr> <td>{{i.id}}</td> <td><a href="/repo/{{ i.id}}">{{i.material_id}}</a></td> <td>{{i.series_title}}</td> <td>{{i.season_title}}</td> <td>{{i.season_number}}</td> <td>{{i.episode_title}}</td> <td>{{i.episode_number}}</td> <td> <input type="checkbox" name="batch" value="{{i.id}}"> </td> </tr> {% endfor %} </table> </form> I am trying to get the data provided from the checkbox and save it in the Batch model. The user selects assets to create a batch, the form returns the … -
Django keep me login feature is not working
I want a solution if any anyone can assist me. When customer will login with keep me login checked then session will not be expire, But if keep me login is not checked then should expire as per bellow mentioned setting.py variable value. So Now login without checked keep me login is working fine as we have set SESSION_SECURITY_WARN_AFTER and SESSION_SECURITY_EXPIRE_AFTER. And if keep me login is checked, I tried to disable by overriding above variable value or increase expire time using request.session.set_expiry(24*30*3600). But it is not working. Can you anyone advice how can I disable this expire time if keep me login is checked. settings.py EXPIRE_TIME = 60*10 SESSION_SECURITY_WARN_AFTER = EXPIRE_TIME SESSION_SECURITY_EXPIRE_AFTER = EXPIRE_TIME + 180 Thanks in Advance. -
Django - how can i filter a list of objects checking a foreign key attribute?
First of all sorry for my bad English. I have to filter a query but using a foreign key attribute. I have a WorkOrder model and have other model with the order movements. this is the model class OrderMovements(models.Model): workorder = models.ForeignKey( WorkOrder, verbose_name=_('order'), ) status = models.IntegerField( choices=FULL_CHOICES, default=1, verbose_name=_('status'), ) timestamp = models.DateTimeField(auto_now_add=True, auto_now=False) Well like you see a work order can have a lot of movements, the queryset that i need to make is a query that take the last 10 WorkOrders that have a OrderMovement with status 3 or 4 included in any of the movements of that WorkOrder. I really don't know how can a make this. Hope you can understand me. Thanks! -
SQL Server - Django incompatibility: SQL generated by Django's count() gets rejected by Sql Server
If an ORM query includes a subquery (easily accomplished by the RawSQL expression or the new Subquery expression in Django 1.11), Django ORM's count method appends that to its group by clause. Here's a simplified example: qs = Activity.objects.filter(...).annotate(feed_date=RawSQL('SELECT MAX(feed_date) FROM core_track WHERE ...')) qs.count() Will generate something like below: SELECT COUNT_BIG(*) FROM (SELECT [core_activity].[id] AS Col1, (SELECT MAX(feed_date) FROM core_track WHERE activity_id=id) AS [feed_date] FROM [core_activity] WHERE (...) GROUP BY [core_activity].[id], (SELECT MAX(feed_date) FROM core_track WHERE activity_id=id)) Normally we use Postgres and it happily accepts & executes this sql. However, our new customer requires using SQL Server which doesn't like a subquery in the group by statement: Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause I wonder if I am overlooking anything, or doing something wrong or otherwise if there's a sane workaround (such as a setting in Sql Server that allows this query to pass). Any suggestions? -
Application installed on HDInsight cluster asks for cluster credentials
I have installed a Django application on my HDInsight cluster, but when I try to access it, it asks me for the cluster credentials first. Is there a way to not let it ask for any cluster credentials and instead directly take me to the application login page? -
'QuerySet' object is not callable
I have a problem with saving data from object to the database This is my code: obj = {} if 'obj' not in request.POST else json.loads( request.POST[ 'obj' ] ) for o in obj: p = testDb( name=str( o ), id=int( obj[o] ) ) p.save() return HttpResponse("Done") Every time I see the same error: TypeError: 'QuerySet' object is not callable in this line: id=int( obj[o] ) Do you know what's the reason ? Thanks, -
Django not loading static files (Pycharm)
I am working on a project which requires loading on a CSS stylesheet and a logo file. But my Django server also returns 404 resource not found while loading these files. I have checked other questions like this but none of them is using the format to specify static files I used. This is the html code i am using {% block stylesheets %} <link rel="stylesheet" type="text/css" href="/static/Emu86/style.css"> {% endblock stylesheets %} This is my settings.py file: """ Django settings for mysite project. Generated by 'django-admin startproject' using Django 1.9.1. For more information on this file, see https://docs.djangoproject.com/en/1.9/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.9/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True # Application definition INSTALLED_APPS = [ 'Emu86', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django_extensions', ] MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'mysite.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, … -
Django Rest Framework, CSRF not Working in POST requests with Postman
My app uses django rest_framework and SessionAuthentication. I can login successfully and have session id and csrf token set in cookie. However, POST request still complains CSRF failure. Login with rest_framework BasicAuthentication; session id and csrf token are set cookie copy and paste csrf token value to Post request header with key "X-CSRFTOKEN" and value from cookie. I test with Postman and got {"detail":"CSRF Failed: CSRF token missing or incorrect."} class ApiLoginView(APIView): authentication_classes = (BasicAuthentication, ) permission_classes = (IsAuthenticated,) def post(self, request, *args, **kwargs): login(request, request.user) user = request.user return Response("login success") class ApiUserView(APIView): authentication_classes = (SessionAuthentication,) permission_classes = (IsAuthenticated,) def post(self, request): return Response("ApiUser Post Success") Is Postman a correct tool for testing? this seems to be a similar problem in Postman Any thing I am missing? and what are the options for me to test django_rest_framework. Sorry it seems to be a common problems but I cannot find work it through after reading related posts. -
Django error : global name 'request' is not defined
I'm getting a little problem with my Django view. I have a function Identity_FewUpdates and I get this error : global name 'request' is not defined. It's pretty strange because I have an other function which looks like very similar and I don't get problems up to now. My function looks like : import requests, os, json, glob from django.shortcuts import render, reverse, get_object_or_404, render_to_response from django.contrib.auth.decorators import login_required from django.http import HttpResponseRedirect, HttpResponse from models import Person, CountryField from BirthCertificate.models import BirthCertificate from forms import PersonForm, PersonForm2 from django.views.generic.edit import UpdateView from django.template.loader import get_template from django.template import Context from xhtml2pdf import pisa import Logger, Search, Folds, Docs, Buffer, EnterSearch from chartit import DataPool, Chart from django.db.models import Count from django_countries import countries import time from random import randint from django.contrib import messages import Global_variables @login_required def Identity_FewUpdates(requests): query_social_number = request.GET.get('social_number') if query_social_number : query_social_number_list = Person.objects.filter(social_number__icontains=query_social_number) print query_social_number_list else : query_social_number_list = Person.objects.none() # == [] form = PersonForm(request.POST or None, instance = query_social_number_list.first()) query_job = request.GET.get('job') context = { "query_social_number" : query_social_number, "query_social_number_list" : query_social_number_list, "query_job" : query_job, "form": form } return render(request, "Update.html", context) The function is a draft for the moment. Do you have any … -
Remove model inheritance and keep id in newly created autofield
I have a model which inherit from a non-abstract super model: class Person(models.Model): pass class User(Person): pass Because of a conception mistake we want to remove the Person model and copy all person data to users. The main problem concern the "id" AutoField. The user model doesn't have any id field autocreated by django because of inheritance the user's primary key is "person_ptr_id". I'm trying to make database migrations. Django migrations want to add the "id" field as AutoField but ask for a default value. I want the default value copied from the person_ptr_id for each user record. I also want the postgres sequence synced with values. Have you already performed such migrations ? -
Error uploading images/documents in wagtail admin
I get an error trying to upload images and documents via the admin in wagtail. All folders and settings are set up correctly. This is what I have figured out so far, something goes wrong in multipart POST. • For a GET request to /admin/documents/multiple/add/ the application receives SCRIPT_NAME=“” (or maybe “/“) and PATH_INFO="/admin/documents/multiple/add/“, as expected. • For a multipart POST request to the same URL, /admin/documents/multiple/add/, the application receives SCRIPT_NAME=“/admin” and PATH_INFO="/documents/multiple/add/“ — which is incorrect. Since Django resolves URLs based on PATH_INFO, this is breaking the Wagtail image/document upload features. -
Django ManagementForm data missing or tampered with when zipping in the view
I'm trying to render a question text in a table to the left column, and the answer form field to the right column. To do that I couldn't figure out any other way than to zip questions and answer_formset in the view and then pass them on to the template. Rendering them works fine, but when submitting the form I get the ['ManagementForm data is missing or has been tampered with'] error. When I don't zip the variables and pass them on separately, submitting works. So did I do the zipping wrong or am I missing something here? View def answerpage(request, pk): AnswerFormSet = formset_factory(AnswerForm, extra=0) questions = Question.objects.filter(questionnaire=pk) question_data = [{'question': question} for question in questions] if request.method == 'POST': answer_formset = AnswerFormSet(request.POST) if answer_formset.is_valid(): for answer_form in answer_formset: if answer_form.is_valid(): instance = answer_form.save(commit=False) instance.answer_text = answer_form.cleaned_data.get('answer_text') instance.question = answer_form.cleaned_data.get('question') instance.save() return redirect('main:calcs') else: answer_formset = AnswerFormSet(initial=question_data) combined = zip(questions, answer_formset) context = { 'combined': combined } return render(request, 'main/questionnaire_details.html', context) And the template #questionnaire_details.html {% extends "main/base.html" %} {% block content %} <form method="post">{% csrf_token %} {{ answer_formset.management_form }} <table> {% for question, form in combined %} <tr><td>{{ question }}</td><td>{{ form.answer_text }}</td></tr> {% endfor %} </table> <input type="submit" … -
Django doesn't load static files
I have an Django project but I have problem with the static files. They are not loaded. In my setting.py I have: STATIC_ROOT = 'static' STATIC_URL = '/static/' ADMIN_MEDIA_PREFIX = STATIC_URL + "grappelli/" STATICFILES_DIRS = (INSTALLED_FOLDER + ('' if DEV_MACHINE else (SALES_COMPANY_CODE_LOWER + '/')) + '/Autralis/libStatic',) I did next to load my styles: ln -s /data /users/my_name/Projects/autralis/Autralis/libStatic/data ln -s /users/my_name/Projects/autralis/Autralis/libStatic /users/my_name/Projects/autralis/Autralis/static And I get static folder as follows: The links to styles in the base template are as follows : <link href="/static/autralis/css/base.css" rel="stylesheet" type="text/css" /> and if I click on the link a get page not found as follows: Any idea how to solve this? -
Trying to run Django tests, get error loading either pysqlite2 or sqlite3?
I am trying to run tests on a project that is not mine when i python manage.py test, I get raise ImproperlyConfigured("Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s" % exc) django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named '_sqlite3' I have installed the sqlite3 and pysqlite2 and all the dev-lib packages, it tells me the requirements are already satisfied. I installed them outside of the virtual env, I believe this is my problem, but I am afraid to use sudo apt-get remove because last time I used this command I deleted almost my entire Ubuntu. Any suggestions? -
foorloop.counter always reset its value when move to anothe rpage?
i have a list and for serial no i am using {{ foorloop.counter }} but it always reset to 1 when we move on to next page. my view :- def university_list(request): totalCount = None search_by_list = filterConfig.university_filter object_name = "university" university_status = "universityStatus" page_size = utility.setting_pagination(request.GET.get('page_size')) page = request.GET.get('page') select_option_value = request.GET.get('type') search_value = request.GET.get("q") university_all_list = Universities.objects.using("cms").order_by('-modifiedAt').all() if select_option_value is not None and select_option_value != '' and search_value is not None and search_value != '': search_filter = utility.create_search_filter_query_by_filter_list_key(search_by_list, select_option_value) university_all_list = university_all_list.filter(**{search_filter: search_value}) totalCount = university_all_list.filter(**{search_filter: search_value}).count() university_all_list = utility.global_pagination(university_all_list, page, page_size) return render(request, 'templates/university/university_list.html', {'data_list': university_all_list, 'object_name': object_name, 'search_by_list': search_by_list, 'select_option_value': select_option_value, 'page_size': page_size, 'page': page, 'totalCount': totalCount,'university_status':university_status}) this is my view a su can see i am getting page and page_size so i mathematically come to conclusion how to do it but dont know hot to do this on templates. for x in range(1,page_size+1): if page: serial_no = page_size*(page-1)+x else: serial_no = x print serial_no but it shows some error unsupported operand type(s) for -: 'unicode' and 'int' and i am not that hopefull that it will work.can some one suggest some other way out to this. -
os.path.join() gives InMemoryUploadFile error
I have written a POST api which posts a file. I want to open the file and parse a JSON content in the file. But the line os.path.join() doesnt seem to work. Have included the code used below. def schedule_load(file_name): print file_name file_json = default_storage.open(os.path.join("schedule_files", file_name), 'r') var = file_json.read() print var file_json.close() schedule = json.loads(var) My POST method : @api_view(['POST']) def post_schedule(request): print "post_schedule" if request.method == 'POST': print "if POST" form = ScheduleForm(request.POST, request.FILES) file_name = form['schedule_file'].value() if form.is_valid(): print "is_valid" form.save() schedule_load(file_name) return JsonResponse({"status": "success"}, status=status.HTTP_200_OK) else: return JsonResponse({"status": "Invalid file"}, status=status.HTTP_200_OK) -
Making multiple calls to a function in Django/Python
I have a list of parameters which I will be using to collect response from a function. I am calling the function over an HTTP post request in Django. As you can see post request will be handling multiple requests in a loop or thread and probably it will take some time as the function performs intensive calculations. My question is, what are the other possible ways, something like job scheduler in which I can submit the array of parameters and I could call the function, record the response of the function in the database and notifies if the whole list of parameters is processed. Is celery is something which can do this or is there any other approach? Initially, I am using threading to calculate response from all the functions.