Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
DJango form.is_valid() function only clean the first form in an if statement
I notice that forms.is_valid only clean form for the first form in an if statement. Example code : if not user_detail_form.is_valid() and not user_location_form.is_valid(): return redirect('account:profile') Accessing cleaned data of user_detail_form works but when accessing cleaned data of user_location_form, it throws an error stating that cleaned_data does not exists. My question is What causes this? and is there a workaround for this? -
Django Nginx Proxy - Static content served with wrong mime type
I have a Django website setup on server using the following guide: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps Gunicorn is the server for the site, with nginx acting as a reverse proxy for serving static content. For some reason css and image files are being sent back to the browser with the "text/html" mime type in the response. How do I fix this? nginx.conf user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; default_type application/octet-stream; include /etc/nginx/mime.types; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } sites-available/project (Project configuration file) server { server_name ip_address; # actual ip is hidden here. access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location /static/ { autoindex on; alias /webapps/dragvenv/static/; } … -
Heroku Deployment - PermissionError: [Errno 13] Permission denied: '/etc/passwd-'
Can anyone help me with this error? I don't what's wrong with the code. is it a database credentials error? Is it an application code error? Is it a Linux/Unix related error? This is my first time to deploy my application on Heroku but I got this error message: PermissionError: [Errno 13] Permission denied: '/etc/passwd-' python ph_dorms/manage.py collectstatic --noinput Traceback (most recent call last): File "ph_dorms/manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line utility.execute() File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv self.execute(*args, **cmd_options) File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute output = self.handle(*args, **options) File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 189, in handle collected = self.collect() File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect handler(path, prefixed_path, storage) File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 353, in copy_file with source_storage.open(path) as source_file: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/files/storage.py", line 33, in open return self._open(name, mode) File "/app/.heroku/python/lib/python3.6/site-packages/django/core/files/storage.py", line 218, in _open return File(open(self.path(name), mode)) PermissionError: [Errno 13] Permission denied: '/etc/passwd-' ! Error while running '$ python ph_dorms/manage.py collectstatic --noinput'. See traceback above for details. You may need to update application code to resolve this error. Or, you can disable collectstatic for this application: $ heroku config:set DISABLE_COLLECTSTATIC=1 https://devcenter.heroku.com/articles/django-assets ! Push rejected, failed to compile Python … -
Connection error while indexing in Elastic search in aws
I want to use elastic search from aws for my usage. Following code works totally fine in my local elastic search but always give error when trying to connect to aws elasticsearch service. I am using python 2.7, django 1.10 and elastic search 5.1.1. Following is the error ConnectionError(HTTPSConnectionPool(host='https', port=443): Max retries exceeded with url: //search-test-abc-jlpfzhi64qcrhhqxycov5rzgcq.ap-south-1.es.amazonaws.com/:443/test-index/tweet/1 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f1e2e5e2090>: Failed to establish a new connection: [Errno -2] Name or service not known',))) caused by: ConnectionError(HTTPSConnectionPool(host='https', port=443): Max retries exceeded with url: //search-test-abc-jlpfzhi64qcrhhqxycov5rzgcq.ap-south-1.es.amazonaws.com/:443/test-index/tweet/1 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f1e2e5e2090>: Failed to establish a new connection: [Errno -2] Name or service not known',))) Also, here is the code that i am using host = AWS_ELASTIC_SEARCH_URL awsauth = AWS4Auth(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ELASTIC_SEARCH_REGION, 'es') es = Elasticsearch( hosts=[{'host': host, 'port': 443}], http_auth=awsauth, use_ssl=True, verify_certs=True, connection_class=elasticsearch.RequestsHttpConnection ) doc = { 'author': 'kimchy', 'text': 'Elasticsearch: cool. bonsai cool.', 'timestamp': datetime.now(), } res = es.index(index="test-index", doc_type='tweet', id=1, body=doc) It is giving error in last line. Also I have made full access to the elastic search url. -
id missing for session after loaddata from sqlite3 to mysql
I'm having this error message after running dumpdata command from sqlite3 database and using loaddata to store the json fixtures into mysql database. I can't seem to figure out why. It work fine on the sqlite3 database but errors on the mysql database. Please any help will be greatly appreciated. I checked the mysql database and there is no id field present in the django_session table I've ran makemigrations and migrate command and these don't work. Just in case anyone wants to post these as answers. Cannot resolve keyword 'id' into field. Choices are: expire_date, session_data, session_key I'm running python 3 and Django 1.10 -
How to show my data inside the template code?
I'm trying to display my content to the template page by django. here is the template code: <html> <body> {% if latest_question_list %} <ul> {% for question in latest_question_list %} <li><a href="/projectapp/{{question.id}}/">{{ question.question_text }}</a></li> {% endfor %} </ul> {% elae %} <p>No polls are available.</p> {% endif %} </body> </html> here view.py page code: def index(request) latest_question_list = Question.object.order_by('-pub_date')[:5] template = loader.get_template('projectapp/index.html') context = { 'latest_question_list':latest_question_list, } return HttpResponse(template.render(context, request)) -
How to read a text file from within Django view function?
I have the following code in one of my view functions: def results(request): data_file = open('data.txt', 'r') data = data_file.read() context = {'rooms': data} return render(request, 'javascript/results.html',context) The file 'data.txt' is located in the same folder as my "views.py". However, I am getting "FileNotFoundError at /results" error. My 'results.html' looks like this: <p> {{ rooms }}</p> What is the correct way to pass data from a text file to a Django view function, and then display the data in the template? Should I use static files instead? -
Django edit user profile after success registration
I try to configure the project in such a way that the user goes through the primary registration, then logged in and from the personal cabinet could add additional information about himself. There are no problems with the initial registration, but when you try to change and supplement the information, there are no changes. My UserProfile model: models.py from customuser.models import User class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) name = models.CharField(max_length=50, blank=True) surname = models.CharField(max_length=50, blank=True, default = None) avatar = models.ImageField(upload_to = 'images/profile/%Y/%m/%d/', blank=True, null=True) position = models.ForeignKey('Position',blank=True, default=None) role = models.ForeignKey('Role', blank=True, default=None) company = models.ForeignKey('Company',blank=True, default=None) status = models.ForeignKey('Status', blank=True, default=None) @receiver(post_save, sender=User) def create_or_update_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) instance.profile.save() forms.py from django import forms from django.forms import ModelForm from client.models import Profile class UserProfileForm(ModelForm): class Meta: model = Profile exclude = ['user'] views.py from customuser.models import User from client.models import Profile from .forms import UserProfileForm def edit_user(request, pk): user = User.objects.get(pk=pk) form = UserProfileForm(instance=user) if request.user.is_authenticated() and request.user.id == user.id: if request.method == "POST": form = UserProfileForm(request.POST, request.FILES, instance=user) if form.is_valid(): update = form.save(commit=False) update.user = user update.save() return HttpResponse('Confirm') else: form = UserProfileForm(instance=user) return render(request, 'client/edit.html', {'form': form}) -
django URLconf current path didn't match
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: polls/ [name='index'] admin/ The current path, polls/, didn't match any of these. My code is... from (app)polls urls.py from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ] from (project)mysite urls.py from django.urls import include, path from django.contrib import admin urlpatterns = [ path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ] from views.py project file from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("Hello world, you are at the polls index.") -
Django REST Framework - TypeError when using POST method
I am new to Django and trying to write an API using Django REST framework to be able to pass some data back and forth, using http POST method (either in JSON or other formats.). My models.py looks like this : from django.db import models from django.contrib.auth.models import AbstractUser from django.contrib.postgres.fields import ArrayField # Create your models here. class Config_Table(models.Model): entity_name = models.TextField(primary_key=True) category = models.TextField() description = models.TextField(blank=True,default='') class UserProfile(AbstractUser): "The Profile of a user with details are stored in this model." username = models.TextField(primary_key=True, max_length=11) first_name = models.TextField(max_length=50,blank=True,default='') last_name = models.TextField(max_length=100,blank=True,default='') # The default username is phone number (Ver. 1.0) phone_number = models.TextField(max_length=11,default=str(repr(username))) avatar = models.ImageField(default='../Static/1.jpeg') GENDER_CHOICES = ( ('M','Male'), ('F','Female'), ) gender = models.CharField(max_length=1,choices=GENDER_CHOICES, default='M') city = models.TextField(max_length=25, blank=True, default='NY') interests = models.ManyToManyField(Config_Table) def __str__(self): return str(UserProfile.username) Config table has been populated with some intitial data. My views.py looks like this : @api_view(['GET', 'POST']) def user_list(request): """ :param request: :return: List of all users, or create a new user """ if request.method == 'GET': userprofiles = UserProfile.objects.all() target_users = [] for user in userprofiles.iterator(): if user.is_superuser == False: target_users.append(user) serializer = UserProfileSerializer(target_users ,many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': # data = JSONParser.parse(request) serializer = UserProfileSerializer(data=request.data) … -
django-paypal suddenly stop recieving
I'm working on Paypal integration to my Django project using Django-Paypal package.I have successfully configured everything and it was working but suddenly the signals form PayPal has stopped. What issue can be occurred? Here's what i have done: from view.py: def payment_process(request): minutes = int(request.user.tagging.count()) * 5 testhours = minutes / 60 hours = str(round(testhours, 3)) # pdb.set_trace() # What you want the button to do. invoice = generate_cid() userInfo = { "name": str(request.user.first_name + ' ' + request.user.last_name), "hours": str(hours), "taggedArticles": str(request.user.tagging.count()) } paypal_dict = { "business": settings.PAYPAL_RECEIVER_EMAIL, "item_name": "Certificate of Completion from Nami Montana", "custom": userInfo, "invoice": str(invoice), "amount": "5.00", "notify_url": "https://6fd5e31b.ngrok.io/users/paypal/", # "return_url": "https://6fd5e31b.ngrok.io/users/profile/", "cancel_return": "https://6fd5e31b.ngrok.io/users/cancel/", } print(paypal_dict) # Create the instance. form = PayPalPaymentsForm(initial=paypal_dict) context = {"form": form} return render(request, "users/generateCert.html", context) from urls.py: urlpatterns = [ url('^paypal/', include('paypal.standard.ipn.urls')), url('^profile/buildCertificate/$', views.CertificateProcess.as_view(), name='certificate'), url('^cancel/$', views.payment_canceled, name='cancel'), url('^done/$', views.payment_done, name='done'), url('^process/$', views.payment_process, name='payment'), ] From models.py: def show_me_the_money(sender, **kwargs): ipn_obj = sender custom = ipn_obj.custom # Undertake some action depending upon `ipn_obj`. if ipn_obj.payment_status == ST_PP_COMPLETED: print('Get success signal') user_info = ast.literal_eval(ipn_obj.custom) if int(user_info['taggedArticles']) > 11: # here i need to generate and send a pdf file to the user in a new tab pass else: print('Get fail … -
Attach link to a button in Django
The following Django template code for opening a different page template works fine: <p> <a id="selenium" href="{% url 'javascript:results' %}">Selenium</a> </p> However, I want the link to open upon button click, but the followng code does not work: <button> <a id="selenium" href="{% url 'javascript:results' %}">Selenium</a> </button> How do I attach a link to a button in Django then? -
Pyinstaller windowed and noconsole is not working
I'm using pyinstaller 3.3.1 and python 3.6.My problem is I cant run pyinstaller script in both no-console and windowed mode. My project is a web application built using Django 1.9. Error Log: Error: [<class 'OSError'>, OSError(9, 'The handle is invalid', None, 6, None), <traceback object at 0x03B098A0>] Traceback (most recent call last): File "site-packages\django\core\handlers\base.py", line 149, in get_response File "site-packages\django\core\handlers\base.py", line 147, in get_response File "crumbs_tableau\views.py", line 1603, in parser File "site-packages\django\shortcuts.py", line 67, in render File "site-packages\django\template\loader.py", line 96, in render_to_string File "site-packages\django\template\loader.py", line 43, in get_template django.template.exceptions.TemplateDoesNotExist: helpers/error.html Internal Server Error: / Traceback (most recent call last): File "crumbs_tableau\views.py", line 286, in parser File "crumbs_tableau\views.py", line 248, in mac_list File "subprocess.py", line 336, in check_output File "subprocess.py", line 403, in run File "subprocess.py", line 667, in __init__ File "subprocess.py", line 905, in _get_handles File "subprocess.py", line 955, in _make_inheritable OSError: [WinError 6] The handle is invalid -
what is the best way to test modules in views?
I have gone through this post, but it didn't contain any relevant answer. I am using Django 1.11 , and my views.py is modular (not class based). I want to test views modules (functions) in the shell, in django's python shell. >>> python manage.py shell Is there any preferred way or shall I import views from django in shell or copy the function directly ? What is the best practice for this? -
django-taggit: how to programmatically create tags?
For example: tags_input = "hello, foo, bar, ok" tagset = tag_parser(tags_input) # {'bar', 'foo', 'hello', 'ok'} obj = Post(title=title, tags=tagset) obj.save() The above snippet doesn't seem to work. How can I create tags programmatically? e.g. in views -
How to scrape data from inside Django app
As an exercise, I came up with an idea of the following Django project: a web app with literally one button to scrape room data from Airbnb and one text area to display the retrieved data in a sorted manner. Preferably, for scraping I would like to use Selenium, as there is no API for this page. So the button would somehow need to launch the browser automation. So question number one is: is it possible to launch selenium from a web app? Furthermore, I already have the working script for collecting the data, however I dont't know how to fit it in a Django project: models, views, separate script? My initial idea was to launch the scraping script on button click, then dump retrieved room-related data to database (updating model's Room attributes like "price" and "link" for example) and display the data back in the text area mentioned before. So question two is: is it possbile to launch Python script in a web app on button click, for example by nesting in a Django template? Or would other technologies be required, such as Javascript? I know my question is general, but I am also looking for general advice, not … -
Use Django to programmatically drop and create indexes at runtime
I am writing an app using python and Django that inserts approximately 800 million records across about 1000 tables per day into a PostgreSQL database. I want to drop the indexes on the tables before processing and then recreate them afterwards. I can directly execute on the connection.cursor to achieve this, (e.g. this post provides a good example of dropping indexes) but I was wondering if there is a more elegant solution using the Django Index object to achieve this? I haven't seen anything except declarative uses of the Index object. Any help much appreciated. -
Issue with many to many relationships
I am not able to add one more player to this coach. Essentially a player can have many coaches, and a coach can have many players under him. I tried to do a Many to Many relationships but then I was able to add the same player to the same coach twice. What should I do here ? class CoachPlayer(models.Model): coach = models.OneToOneField('Coach', on_delete=models.CASCADE) player = models.ForeignKey('player.Player', on_delete=models.CASCADE) start_date = models.DateTimeField(auto_now_add=True) -
I searched lot in Django Doc but i did not find useful difference between this self.kwargs['pk'] vs self.kwargs.get('pk', None)?
I searched lot in Django Doc but i did not find useful difference between this self.kwargs['pk'] vs self.kwargs.get('pk', None) please what is the difference between this two in Django -
Create Generator from queryset results - Python?
I am new to python I need to convert this following function to the generator to save memory, how can I achieve that? question = <QuerySet [<Question: foobar?.>]> for index, question in enumerate(questions): if question["pk"] == self.pk: try: next_question = questions[index + 1] if next_question["pk"]: prev_next.update({"next" : next_question["pk"] }) except IndexError: prev_next.update({"complete" : True }) try: if index > 0: previous_question = questions[index - 1] if previous_question["pk"]: prev_next.update({"prev" : previous_question["pk"] }) except IndexError: pass return prev_next -
Django output Fixtures from UserTeams
I am trying to print a list of fixtures for the logged in player, where the fixtures they can view are only those for the teams they are a member of. Basically I want to output the fixtures where the hometeamID/awayteamID = each teamID in UserTeams where the userID = request.user. Fixtures model: class Fixtures(models.Model): fixture = models.CharField(max_length=100) hometeamID = models.ForeignKey(Team,related_name='team1',on_delete=models.CASCADE) awayteamID = models.ForeignKey(Team,related_name='team2',on_delete=models.CASCADE) datetime = models.DateTimeField() homegoals = models.PositiveSmallIntegerField() awaygoals = models.PositiveSmallIntegerField() played = models.NullBooleanField() UserTeams model: class UserTeams(models.Model): userID = models.ForeignKey(User,on_delete=models.CASCADE) teamID = models.ForeignKey(Team,on_delete=models.CASCADE) Fixtures View: def fixturesview(request): query = Fixtures.objects.all() return render(request, 'teammanager/fixtures.html', { "fixtures": query }) HTML: <p>Fixtures</p> {%for team in teams%} <h3>{{fixtures.fixture}}</h3> {%endfor%} -
wkhtmltopdf with pdfkit not working on VPS server
I've installed wkhtmltopdf and xvfb. It's working fine in server with command line as well as in django/python shell but with uwsgi+nginx it throwing the below error wkhtmltopdf exited with non-zero code 127. error: /usr/bin/xvfb-run: 25: /usr/bin/xvfb-run: awk: not found /usr/bin/xvfb-run: 97: /usr/bin/xvfb-run: getopt: not found Django Version: 1.11.6 Exception Type: OSError Exception Value: wkhtmltopdf exited with non-zero code 127. error: /usr/bin/xvfb-run: 25: /usr/bin/xvfb-run: awk: not found /usr/bin/xvfb-run: 97: /usr/bin/xvfb-run: getopt: not found Exception Location: /home/dev/env3/lib/python3.5/site-packages/pdfkit/pdfkit.py in to_pdf, line 159 Python Executable: /usr/bin/uwsgi-core Python Version: 3.5.2 Thanks. -
Receive thumbnail of uploaded video in javascript
I have an upload box which changes its background to the uploaded image via the following code: $('input#id_image').on('change', function(e) { var imagePath = URL.createObjectURL(e.target.files[0]); $('.add_file_label').css('background', 'url(' + imagePath + ') scroll no-repeat center/cover'); }); however I want to be able to change the background when a video is uploaded, using a randomly generated thumbnail. I can already acquire that thumbnail in my Django backend via the following code using Moviepy (instance is the uploaded FileField: def generate_thumbnail(instance): filename = instance.image.path thumbnail = VideoFileClip(filename) name = random_string() + '.png' time = random.randrange(60) thumbnail.save_frame('media/' + name, t=time, withmask=True) instance.thumbnail = name and this thumbnail is used as the thumbnail after the form is submitted. However I need to obtain the thumbnail before the form is submitted as I want the upload box to change its background to the thumbnail as soon as the user selects their uploaded video (similar to how it changes when someone uploads an image). So is there any way to obtain the thumbnail generated from my Django backend? The thumbnail saves in my media folder but that's only after the form has been submitted so it isn't accessible via AJAX. Or could generating the thumbnail in the front-end … -
Customize Python package - django-paypal
I'm working on a project in which I need to make some changes in django-paypal package. I have cloned the package's GitHub repo and make a very simple change (Just change a form submit type=image source) and then install it via python setup.py install but now I couldn't find any path to import this package. For example when I install it by using pip install django-paypal i was able to import it like from paypal.standard.forms import PayPalPaymentsForm but after installing it via python setup.py install from my local changed source then it couldn't find any paypal package. How can i fix this issue? Help me, please! Thanks in advance! -
django data migration type not mach error
I have the following model class Region(models.Model): polygon = models.PolygonField(_("polygon")) name = models.CharField(_("name"), max_length=100) Now I am going to convert PolygonField to MultiPolygonField and for sure I have to convert the current data type from Polygon to MultiPolygon. The migration I have written: def convert_polygon_to_multipolygon(): migrations.RunSQL(""" UPDATE locations.region set polygon_tmp = (Select ST_Multi(polygon) from locations_region) """) class Migration(migrations.Migration): operations = [ migrations.AlterField( model_name='region', name='polygon', field=django.contrib.gis.db.models.fields.MultiPolygonField(srid=4326, verbose_name='polygon'), ), migrations.RunPython(convert_polygon_to_multipolygon), ] In which I change column type and then convert data, but I am facing this error: django.db.utils.DataError: Geometry type (Polygon) does not match column type (MultiPolygon) The other way I tried was to first create a new column with MultiPolygon type, then copy and convert Polygon column data to MultiPolygon in this new column, then remove the old Polygon column and rename the new column to Polygon again, which encounters the same error again. My migraion: def convert_polygon_to_multipolygon(): migrations.RunSQL(""" UPDATE locations.region set polygon_tmp = (Select ST_Multi(polygon) from locations_region) """) class Migration(migrations.Migration): operations = [ migrations.AddField( model_name='region', name='polygon_tmp', field=django.contrib.gis.db.models.fields.MultiPolygonField(srid=4326, verbose_name='polygon', null=True), ), migrations.AlterField( model_name='region', name='polygon', field=django.contrib.gis.db.models.fields.MultiPolygonField(srid=4326, verbose_name='polygon'), ), migrations.RunPython(convert_polygon_to_multipolygon), migrations.RemoveField( model_name='region', name='polygon', ), migrations.RenameField( model_name='region', old_name='polygon_tmp', new_name='polygon', ), ] where is the problem? tnx