Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django query limit 2000 for Postgresql Psycopg2
I have Django app and using postgresql with psycopg2. I observed that there is maximum limit of 2000 records of data which can be retrieved and saved. It might have to do with psycopg2 or postgresql. Is there any way I can increase this limit or make it unlimited? -
How do I use matplotlib to produce plots on Django admin dashboard?
I want to customize the plots myself using matplotlib and show them on the Django admin dashboard. Can anyone tell me how to do this? Thanks! -
Python Matplotlib Django : out of stack space (infinite loop?)
I am trying to generate a graph using matplotlib in django. If I load a large csv file and generate a graph it works as expected. However if I do the same process again I see the below error: TclError: out of stack space (infinite loop?) What is causing this and how can I resolve this issue. Anything comment is welcome. Code: from django.shortcuts import render from django.conf import settings from django.core.files.storage import FileSystemStorage from django.shortcuts import redirect import csv import pandas as pd import matplotlib.pyplot as plt import re import sys import gc GraphName = "" YLabel = "" YColumns = "" XLabel = "" XColumns = "" file = "" ip = "" tmp_file_name = "" def get_form_inputs(request): global GraphName global YLabel global YColumns global XLabel global XColumns global file global ip GraphName = "" YLabel = "" YColumns = "" XLabel = "" XColumns = "" file = "" ip = "" tmp_file_name = "" GraphName = request.POST.get("GraphName") YLabel = request.POST.get("YAxisLabel") YColumns = [request.POST.get("YInputs")] for num in range(1,20): if request.POST.get("YInputs"+str(num)+"") is None: break YColumns.append(request.POST.get("YInputs"+str(num)+"")) XLabel = request.POST.get("XAxisLabel") XColumns = [request.POST.get("XInputs")] file = request.FILES['file'] ip = request.META['REMOTE_ADDR'] def process_validate_csv_file(): global tmp_file_name tmp_file_name = 'tmp_graph'+ip+'.csv' try: with open(tmp_file_name, 'w') … -
Why can't I iterate over this queryset?
I am trying to use a basic for loop to iterate over this query and it is giving me a "Company is not iterable" error. Any help? def activate(request, uidb64, token, pk): try: uid = force_text(urlsafe_base64_decode(uidb64)) company = Company.objects.get(pk=uid) except(TypeError, ValueError, OverflowError, Company.DoesNotExist): company = None if company is not None and account_activation_token.check_token(company, token): pmm_date = Thursday.objects.get(pk=pk) company.is_registered = True company.save() pmm_date.assigned_company = company pmm_date.save() automatic_denial_list = Company.objects.exclude(company).filter(pmm_date=pmm_date) current_site = get_current_site(request) for company in automatic_denial_list: if company.email_one or company.email_two: mail_subject = 'Denied Pizza My Mind Visit' message = render_to_string('thursdays/denied_visit.html', { 'user': company.name, 'domain': current_site.domain, }) to_email = company.email_one if company.email_one else company.email_two email = EmailMessage(mail_subject, message, to=[to_email]) email.send() # company.delete() return redirect('/') else: return HttpResponse('Activation link is invalid!') The error is when I define automatic_denial_list. Thanks! -
ConnectionAbortedError after choosing an image to upload
This is a pretty long question and I really appreciate your time - hopefully most part of the pasted codes you can just skim through. I have been working on a Django project which involved user uploading images (and cropping/rotating the image on the client side, before sending the final image to the server). The JavaScript Image Cropper I found and used is Croppie, with some basic customization. The whole process was working at that time (about 4 months ago, Nov. 2017), but then I set the project aside. I've recently picked it back up, and with some new package updates, I've made some minor modifications to the Django code, but DID NOT touch the JavaScript/HTML part. I was surprised that when I tried to upload an image, the console threw a ConnectionAbortedError and triggered some downstream errors, very much like encountered in this question. The full error log is as follows: Traceback (most recent call last): File "c:\users\xurub\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 138, in run self.finish_response() File "c:\users\xurub\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 180, in finish_response self.write(data) File "c:\users\xurub\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 274, in write self.send_headers() File "c:\users\xurub\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 332, in send_headers self.send_preamble() File "c:\users\xurub\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 255, in send_preamble ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1') File "c:\users\xurub\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 453, in … -
Django "The submitted file is empty"
I have used multiple FileFields in a model and my app gives error "The submitted file is empty" on execution, folowing are the details of my code: My models.py: class KBCTest(models.Model): algorithm = models.CharField(max_length=10, blank=False) entityFile = models.FileField(blank=False, upload_to=updateFilename, validators=[validateTestingFileExtension]) relationFile = models.FileField(blank=False, upload_to=updateFilename, validators=[validateTestingFileExtension]) My forms.py class KBCTestForm(forms.ModelForm): class Meta: model = KBCTest fields = ('algorithm', 'entityFile', 'relationFile') def clean(self): super(KBCTestForm, self).clean() data = self.cleaned_data return data My views.py: def testing(request): title = 'Testing' template = 'testing.html' form = '' if request.method == 'GET': form = KBCTestForm() if request.method == 'POST': form = KBCTestForm(request.POST, request.FILES) if form.is_valid(): form.save() runAlgo = Run(form.cleaned_data) runAlgo.configAndTest() return HttpResponseRedirect(reverse('learning', kwargs={'function': 'testing'})) context = {'title': title, 'form': form} return render(request, template, context) My template: {% if request.user.is_authenticated %} <div class='col-sm-4 col-sm-offset-4'> <h1>{{ title }}</h1> <!-- If form not rendered(i.e. views context) don't show submit button --> {% if form %} <!-- Use 'csrf_token' to prevent cross-site forgery --> <form enctype='multipart/form-data' method='POST', action=''> {% csrf_token %} {{ form|crispy }} </br> {{ form.non_field_errors|crispy }} <input type='submit' value='Test' class='btn btn-default'/> </form> {% endif %} </div> {% endif %} When I run this template it fails to validate the form and gives error "The submitted file is empty" error … -
display list in django template
I have a list of tuples which looks something like the following: answer_guess_list = [('A',), ('B', 'C',), ('A', 'B', 'C', 'D',)] In my templates, I have a template variable with the same name. {% for guess_list in answer_guess_list %} <p> {% for guess_value in guess_list %} {{ guess_value }}, # Notice the comma (,) {% endfor %} </p> {% empty %} <p>Nothing to show.</p> {% endfor %} It displays the list as the following: A, B, C, A, B, C, D, I don't want the terminal commas and rather display it as A B, C A, B, C, D Please suggest changes or provide hints to achieve the same. -
django-extensions shell_plus error - ImportError: cannot import name 'Type'
I'm using a venv in Django with Python 3.5 and Django extensions. When I try and run python manage.py shell_plus i get the following error: Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line utility.execute() File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django/core/management/__init__.py", line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django/core/management/__init__.py", line 216, in fetch_command klass = load_command_class(app_name, subcommand) File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django/core/management/__init__.py", line 36, in load_command_class module = import_module('%s.management.commands.%s' % (app_name, name)) File "/Users/lucassalmins/.pyenv/versions/3.5.1/lib/python3.5/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 986, in _gcd_import File "<frozen importlib._bootstrap>", line 969, in _find_and_load File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 673, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 662, in exec_module File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django_extensions/management/commands/shell_plus.py", line 14, in <module> from django_extensions.management.shells import import_objects File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django_extensions/management/shells.py", line 5, in <module> from typing import ( # NOQA ImportError: cannot import name 'Type' After plenty of googling, I'm struggling to understand the issue. -
why is this not working writing ValueError (though i used Tango_with_django book 1.9)?
Am using Django 2.0.2 for Django 1.9 Tutorial and could figure out what the problem was after reconstruct my URL. Error Display (The view rango.views.category didn't return an HttpResponse object. It returned None instead.) from django.template import RequestContext from django.shortcuts import render_to_response from rango.models import Category, Page def index(request): # Obtain the context from the HTTP request. context = RequestContext(request) # Query the database for a list of ALL categories currently stored. # Order the categories by no. likes in descending order. # Retrieve the top 5 only - or all if less than 5. # Place the list in our context_dict dictionary which will be passed to the template engine. # Query for categories - add the list to our context dictionary. category_list = Category.objects.order_by('-likes')[:5] context_dict = {'categories': category_list} # looping through each category returned, and create a URL attribute. # This attribute stores an encoded URL (e.g spaces replaced with underscores). for category in category_list: category.url = category.name.replace(' ', '_') # Render the response and return to the client! return render_to_response('rango/index.html', context_dict, context) def category(request, category_name_url): # Request our context from the request passed to us. context = RequestContext(request) # Change underscores in the category name to spaces. … -
Populate Model With Initial Data
I am fairly new to Django, and am entirely at a loss on this question. I am creating a website scraper that will scrape data off of a website once a day and save it to the database. However, the website in question has kept a history of all their data. I want to populate the database with this historical data. I was thinking of scraping the historical data and saving it to a .txt file. But then I don't know what to do next? How do I populate my model with inital data set? -
Elasticsearch, save mail and search
I get mail import imaplib mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login('myusername@gmail.com', 'mypassword') mail.list() # Out: list of "folders" aka labels in gmail. mail.select("inbox") # connect to inbox. How can I save Title, Date, Body, Author in Elasticsearch? I will search mail by keys. -
user login error unicode object not callable
I created MainUser model and this model has unicode method, but here I have problem like 'unicode' object is not callable this my authorization function: def auth(request): params = dict() if request.method == 'POST': try: login = request.POST['login'] password = request.POST['password'] except: messages.add_message(request, messages.WARNING, 'empty fields') return redirect(reverse('main:sing_in')) user = authenticate(username=login, password=password) if user is not None: login(request, user) return redirect('main:work') else: return redirect(reverse('main:sing_in')) return render(request, 'sing_in.html', params) and this one my mainuser model: class MainUser(AbstractBaseUser, PermissionsMixin): """ django user model """ login = models.CharField(max_length=20, blank=False, unique=True, db_index=True, verbose_name=u'Логин') first_name = models.CharField(max_length=222, blank=True, verbose_name=u'Имя') second_name = models.CharField(max_length=222, blank=True, verbose_name=u'Фамилия') email = models.EmailField(max_length=100, blank=True, verbose_name=u'email') is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) objects = MainUserManager() USERNAME_FIELD = 'login' REQUIRED_FIELDS = [] def full(self): return { "first_name": self.first_name, "second_name": self.second_name, "email": self.email } @property def is_staff(self): return self.is_admin def get_short_name(self): return self.login def get_full_name(self): return u"{0} {1}".format(self.second_name, self.first_name) def __unicode__(self): return u"{}".format(self.login) class Meta: verbose_name = u"Пользователь" verbose_name_plural = u"Пользователи" I can't understand where bug in the code, it give error when i call login(request, user) -
Store additional information in Django session in login response
After a user logs in, they're sent the session cookie inside an HttpResponse object. I want to add an additional field 'foo' to the session, just like I had done request.session['foo'] = 'bar' The above doesn't work because the login request itself doesn't have the session object itself, only subsequent requests have the cookie. How can I do this? -
(Django) How to add a queryset count for dynamic objects in a list to a template?
I am building a website with django as a place to store my university notes. I have a notes model which is linked by foreignkey to a categories model. On my homepage I use a for loop to post a link to each category page, but in brackets I also wanted to display the number of published notes in that category. For example: Biology (6) Chemistry (4) Physics (12) etc etc I use template tag to give the length eg. {{category.notes_set.all|length}} within the for loop to display the number of notes in a category, but this ignores whether the notes are published or whether they are just created. It would give a value of 7 if I had 6 published and 1 unpublished note - I want it to display 6. I really want to filter(published_date__lte=timezone.now()) but don't think this can be achieved in template. Do I have to create a context dictionary for every single category and annotate it with the count within the view? I feel this would be unmanageable when the number of categories and sub-categories becomes very large. Can I do this as a for loop within views.py? Sorry if this has an obvious answer, I … -
Django bulk create for non repeatative
I want to insert data from excel file to the databse but i want to insert only non repeatative ones i wrote this code but the if statement is always false! ef ca_import(request): uploadform=UploadFileForm(None) if request.method == 'POST' : uploadform = UploadFileForm(request.POST, request.FILES) if uploadform.is_valid(): file = uploadform.cleaned_data['docfile'] workbook = openpyxl.load_workbook(filename=file, read_only=True) # Get name of the first sheet and then open sheet by name first_sheet = workbook.get_sheet_names()[0] worksheet = workbook.get_sheet_by_name(first_sheet) data = [] try: for row in worksheet.iter_rows(row_offset=1): # Offset for header stockname =StocksName() if (StocksName.objects.filter(name=row[0].value).count()<1): stockname.name=row[0].value data.append(stockname) messages.success(request,"Successful" ,extra_tags="saveexcel") except : messages.error(request,_('Error'),extra_tags="excelerror") return render(request, 'BallbearingSite/excelfile.html',{'uploadform':uploadform}) StocksName.objects.bulk_create(data2) any suggestion to solve it ? thanks in advance -
How to display database timestamp in django, sqlite and python
I'm running Django with SQLite, and creating a log. I save the results of timezone.now() in the database, and when I retrieve it, it looks like this example: 2018-03-02 17:44:17.868267 All very well, but 1) this does not say the timezone, and it's not UTC. I happen to know it's in America/Los Angeles time. 2) I want to pretty-print the timestamp, and I'm getting lost in all the documentation that's about naive and aware and pytz and what seems like a bunch of things even further from my needs. So: presuming all the action remains in the same time zone, what functions are usable for formatting this time. For instance with day of the week, Month names, AM/PM and so on. -
How to add Group field to Custom User?
i used this Code in serializer.py group = Group.objects.get() group.user_set.add(self.object) it work fine when the Group Field have just one item and added in DB with no problem. But when i add more than one item in The Group List Field i am getting an Error: get() returned more than one Group -- it returned 2! what can i do ? -
Adding image effects in django (like mosaic)?
How can I add different effects to the image uploaded in django.? Effects like mosaic, sketch, etc... -
How to get the data from Django form and store on different variable every time user submit
views.py def post(self,request): form = HomeForm(request.POST) if form.is_valid(): I want to get the data from this fiels named post and save in different variables text = form.cleaned_data['post'] form = HomeForm() return render(request, self.template_name, {'form': form}) -
Make parallel concurrent calls to external apis from django
I have a django app (django+python+apache mod_wsgi) which act as a middleware between two systems, app gets requests from one system and it makes multiple requests to other apis to fetch and prepare the desired response, and then it passes the response to the requesting system. So it's basically acting as a one-to-many middleware, problem is making calls sequentially is taking too much time, I tried to use threading for IO concurrency. However this is not working (I've read django works on single thread; correct me if I'm wrong), I've not worked with making requests in parallel on a webserver and have no idea how to do it. Following is the current implementation : with futures.ThreadPoolExecutor(max_workers=MAX_BATCH_SIZE) as executor: future_to_url = {} for pnode in plist_node: config = {'url':rurl} futur = executor.submit(self.get_result_from_url, config) Can someone please suggest the right way to do this ? -
Any /polls URL always call index() function in views.py
polls/urls/py from django.conf.urls import url from . import views urlpatterns = [ url('', views.index, name='index'), url('<int:question_id>/', views.detail, name='detail'), # ex: /polls/5/results/ url('<int:question_id>/results/', views.results, name='results'), # ex: /polls/5/vote/ url('<int:question_id>/vote/', views.vote, name='vote'), ] views.py from __future__ import unicode_literals from django.http import HttpResponse from .models import Question from django.template import loader # from django.shortcuts import render def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] template = loader.get_template('polls/index.html') context = { 'latest_question_list': latest_question_list, } return HttpResponse(template.render(context, request)) def detail(request, question_id): return HttpResponse("You're looking at question %s." % question_id) def results(request, question_id): response = "You're looking at the results of question %s." return HttpResponse(response % question_id) def vote(request, question_id): return HttpResponse("You're voting on question %s." % question_id) url=http://127.0.0.1:8000/polls url=http://127.0.0.1:8000/polls/1/ url=http://127.0.0.1:8000/polls/1/results These all urls are giving same mapping same function index(). Any help will be appreciated -
Concat Queryset Based On Condition?
let's say i have models like follow Question and Answer class Question(models.Model): category = models.ForeignKey(Category) class Answer(models.Model): question = models.Foreignkey(Question) category = models.ForeignKey(Category) my queryset like follow questions = Question.objects.filter(category_id=1) answers = Answer.objects.filter(category_id=1) The questions queryset have all the questions for category and answers queryset have all the answers for questions per user. now I want to display all the questions with answers and warning messages to unanswered question, what is the best way to achieve this? -
Django direct assignment to the reverse side of a many-to-many set is prohibited
I am new to Python and Django. I am building APIs using Django REST framework and getting following error when I call API for MyModel. I am getting following error, after I created a ManyToMany relation Direct assignment to the reverse side of a many-to-many set is prohibited. Use label_id.set() instead. class Label(models.Model): label_id = models.CharField(db_column='labelId', primary_key=True, max_length=255) name = models.CharField(unique=True, max_length=255, blank=True, null=True) class Meta: app_label = 'my_app' managed = False db_table = 'label' class MyModel(models.Model): my_model_id = models.CharField(db_column='myModelId', primary_key=True, max_length=255) is_deactive = models.NullBooleanField(db_column='isDeactive', blank=True, null=True, default=False) labels = models.ManyToManyField(Label, through="MyModelLabel", related_name='label_id') class Meta: app_label = 'my_app' managed = False db_table = 'my_model' class MyModelLabel(models.Model): my_model_id = models.ForeignKey(MyModel, on_delete=models.CASCADE, db_column='myModelId', related_name='labels') label_id = models.ForeignKey(Label, on_delete=models.CASCADE, db_column='labelId', primary_key=True, related_name='labels') class Meta: app_label = 'my_app' managed = False db_table = 'my_model_label' unique_together = (('myModelId', 'labelId'),) serializers.py class LabelSerializer(ModelSerializer): class Meta: model = models.Label fields = '__all__' class MyModelLabelSerializer(ModelSerializer): class Meta: model = models.MyModelLabel fields = '__all__' class MyModelSerializer(ModelSerializer): labels = MyModelLabelSerializer(many=True, read_only=True) class Meta: model = models.MyModel exclude = ('is_deactive',) view.py class MyModelViewSet(viewsets.ReadOnlyModelViewSet): serializer_class = serializers.MyModelSerializer queryset = models.MyModel.objects.all().exclude(is_deactive='1') Django Version: 2.0.2 Python Version: 3.6.3 -
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name
I want to use the default django.contrib.auth.views for resetting passwords with email confirmation. All this code is on urls.py : from django.conf.urls import url from . import views from django.contrib.auth import views as auth_views app_name = 'houses' urlpatterns = [ # Root and details page url(r'^$', views.index, name='index'), url(r'^(?P<house_id>[0-9]+)/$', views.view_house, name='index'), # Register / login / logout url(r'^register/$', views.UserFormView.as_view(), name='register'), url(r'^login/$', views.login_user, name='login_user'), url(r'^logout/$', views.logout_user, name='logout_user'), # User profiles and edit profiles url(r'^profile/$', views.view_profile, name='view_profile'), url(r'^profile/edit/$', views.edit_profile, name='edit_profile'), # ---- ERRORS ARE HERE ---- change / reset passwords url(r'^change_password/$', views.change_password, name='change_password'), url(r'^password_reset/$', auth_views.password_reset, {'post_reset_redirect': 'houses:password_reset_done',}, name='password_reset'), url(r'^password_reset/done/$', auth_views.password_reset_done, name='password_reset_done'), url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', auth_views.password_reset_confirm, name='password_reset_confirm'), ] No matter what I try i keep getting: Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name. Is it because my app name is houses? I've been trying things for hours with no luck. -
Ajax/ jquery way of checking from template if celery task is complete
I am using django + celery + RabbitMq(broker) + Redis(backend). Here's the problem: I am analysing a test, which depending on number of test takers might take a long time, hence i am using celery to do it asynchronously. Here is the view.py code: new_rl = teacher_test_analysis_new.delay(test_id,user.id) te_id = new_rl.task_id res = AsyncResult(te_id) if res.state != "PENDING": # fetch data from database and return to template new_rl = SscTeacherTestResultLoader.objects.get(test__id =test_id) return HttpResponse(new_rl) else: # return a loading gif Now the only problem is , most of the times loading gif is returned as task is still PENDING, and it gets stuck there even when task gets finished. What I want to do is ask AJAX or jquery in my template to check every few milliseconds if the task is complete and eventually load the data when it finally gets complete. How to do that?