Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
"ailed to create process" when make new virtual environtment python
i using python 3.6.4 and only use it. I have install pip, v i r t u a l e n v, and v i r t u a l e n v w r a p p e r - w i n, but when i make new virtual environment using v i r t u a l e n v d j a n g o - e x a m p i have try use m k v i r t u a l e n v d j a n g o - e x a m p too, but both of it not worked, i have check it with pip freeze the result is good, i have installed all of them, but why i cannot make new virtual environment (i got message "failed to create process")? -
It is possible to check in django rest query filter using .upper()
How to compare value in db with request.data after changing both to upper case str_code = CustomerAccount.objects.filter(strAccountCode.upper() = request.data['strAccountCode'].upper(),chrDocumentStatus='N') -
What is the effective way of managing threads?
I am building a ToDo web-app in Django. This app will create a reminder and will send a text-based SMS to the given phone number. So I decide the given below algorithm: Take user input of Time and Reminder. Validate the input. If valid, store in database and create a thread for sending SMS. Else, display an error message. This algorithm was working well until I came across an article in which it has said that we should not use Threads in web app cause it will use a lot of resources. This is true for my app because I am planning to use my app in such a way that a user can create only 100 reminders. This will create 100 threads on my app per user. I decided to develop a Priority Queue which will insert these 100 threads based on the priority of Time user has provided as input. Now my question is : Does creating a Thread and putting it in a Queue decreases the load on CPU or not? If not, what is the alternative? NOTE : I already know about CELERY and REDIS. But I decided to go this way because I want to … -
Django - Simple User Register not working [Not saving to DB]
I have simple user registration page, but its not saving to the database. Issue: Register form displays, upon clicking submit button, doesn't do anything! Stays there (it should show a HttpResponse('User Registered') And save to db I've know I'm missing something, but what? ideas please. Views.py from django.shortcuts import render from django.views.generic import CreateView from website.forms import RegisterUserForm from django.http import HttpResponseForbidden # Create your views here. class RegisterUserView(CreateView): form_class = RegisterUserForm template_name = "account/register.html" # Overide dispatch func - check if the user is authenticated or return forbidden def dispatch(self, request, *args, **kwargs): if request.user.is_authenticated(): return HttpResponseForbidden() return super(RegisterUserView, self).dispatch(request, *args, **kwargs) # Overide form valid method behaviour def form_valid(self, form): # False commit - don't save to db, locally cleaning users password first user = form.save(commit=False) user.set_password(form.cleaned_data['password']) user.save(); # Change to redirect - later one! return HttpResponse('User Registered') def Home(request): return render(request, 'account/home.html') Register.html {% extends 'base.html' %} {% block title %} Register {% endblock %} {% block head %} {% load static %} <link href="{% static 'forms.css' %}" rel="stylesheet"> {% endblock %} {% block heading %} <h1>Register</h1> {% endblock %} {% block body %} <div class="bg-contact2" style="background-image: url('images/bg-01.jpg');"> <div class="container-contact2"> <div class="wrap-contact2"> <form class="contact2-form validate-form"> <span class="contact2-form-title"> … -
Download a file using Django rest framework by authenticating the user
I am working on a project where I am using Django as the back end. I am working with Django rest framework, and I have an API to download a File. @detail_route(methods=['GET'], permission_classes=[IsAuthenticated]) def test(self, request, pk=None): try: ticket = Ticket.objects.get(id=pk, user=request.user) file_path = ticket.qrcode_file.path if os.path.exists(file_path): with open(file_path, 'rb') as fh: response = HttpResponse(fh.read(), content_type="image/jpeg") name = "%s %s %s %s.jpg" % (ticket.show.title, datetime.strftime(ticket.show.date_time, "%H_%M_%p"), datetime.strftime(ticket.show.date_time, "%d %B %Y"), ticket.id) response['Content-Disposition'] = "attachment; filename=%s" % name.replace(" ", "_") return response return Response({'error': 'Ticket doest not belong to requested user.'}, status=status.HTTP_403_FORBIDDEN) except Ticket.DoesNotExist as e: return Response({'error': str(e)}, status=status.HTTP_404_NOT_FOUND) On the front-end I am using Nuxtjs (ssr for vuejs). This is a little snippet of code, where a user can download the file by clicking a link of target blank.: <a class="downloadBtn" target="_blank" :href="`${baseURL}/payments/api/tickets/${ticket.id}/download_ticket/`">Download e-ticket</a> The web app is running on Nuxtjs server (localhost:3000) and Django server is running on localhost:8000, only the API is used to communicate between the Nuxtjs and Django by using the auth token. When I click the download link it opens up a new tab and make a request from that new tab, where no token is passed with the request. And since, the django … -
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