Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Is it possible to disable Django two-factor authentication with a setting?
I'm trying to write a LiveServerTestCase for a website which is protected with Django Two-Factor Authentication. What I have so far is: import os from urllib.parse import urljoin from django.urls import reverse from django.conf import settings from lucy_web.test_factories import UserFactory from django.contrib.staticfiles.testing import StaticLiveServerTestCase from selenium import webdriver chromedriver_path = os.path.join(os.path.dirname(os.path.dirname(settings.BASE_DIR)), 'chromedriver') assert os.path.isfile(chromedriver_path), f"There should be a chromedriver executable at {chromedriver_path}" class SeleniumTest(StaticLiveServerTestCase): @classmethod def setUpClass(cls): super().setUpClass() cls.driver = webdriver.Chrome(chromedriver_path) cls.password = 'foobar' # Set the password here to avoid using the hashed attribute cls.user = UserFactory(password=cls.password, is_superuser=True) def test_login(self): url = urljoin(self.live_server_url, reverse('dashboard:families')) self.driver.get(url) self.driver.find_element_by_name('auth-username').send_keys(self.user.username) self.driver.find_element_by_name('auth-password').send_keys(self.password) self.driver.find_element_by_xpath('//input[@value="Next"]').click() The problem is that at this point, the driver arrives at a two-factor authentication login page in which it is required to scan a QR code: This is too involved to do using the test browser, so I'd like to use override_settings to disable the two-factor authentication for testing purposes. However, looking at the General Settings of Django two-factor authentication, I wasn't able to find a setting which disables it. Is there any way I could disable two-factor authentication to continue with this live server test case? -
Django - Using PostGIS database with PostgreSQL database, do I need 2 databases?
I'm currently using a single PostgreSQL database, with standard settings. DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'postgres', 'USER': 'postgres', 'PASSWORD': password, 'HOST': 'localhost', 'PORT': '', } } My question is, can I keep using the default postgres setup, and just perform CREATE EXTENSION postgis in the shell to get access to postgis features? Or do I need to add a postgis database separately, like below: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'postgres', 'USER': 'postgres', 'PASSWORD': password, 'HOST': 'localhost', 'PORT': '', } 'geodata': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', 'NAME': 'geodjango', 'USER': 'geo', }, } -
Saving Django Form With Foreign Key Reference
I have a form which references three models and I want to save the items to each model. One of the models has foreign key references to the other models and I want to save them too. My models look like: class Address(models.Model): housenumber = models.CharField(max_length=20,default='',blank=True) street = models.CharField(max_length=80,default='',blank=True) town = models.CharField(max_length=80,default='',blank=True) county = models.CharField(max_length=60,default='',blank=True) country = models.CharField(max_length=20,default='',blank=True) postcode = models.CharField(max_length=10,default='',blank=True) class GeoLocation(models.Model): longitude = models.FloatField(default=-4.2576300) latitude = models.FloatField(default=55.8651500) class Location(models.Model): locationname = models.CharField(max_length=80,default='',blank=True) address = models.ForeignKey(Address, on_delete=models.CASCADE) geolocation = models.ForeignKey(GeoLocation, on_delete=models.CASCADE, default='') My views looks like: if locationform.is_valid() and addressform.is_valid() and geolocationform.is_valid(): locationform.save(commit=False) new_address = addressform.save() new_geolocation = geolocationform.save() locationform.address = new_address locationform.geolocation = new_geolocation locationform.save() This will give me an error that states "NOT NULL constraint failed: location_location.address_id". Can anyone help? I am new to Django so find this stuff hard. -
E-mail 'forgot my password' don't work
I'm trying to use: path('esqueci_senha/', auth_views.password_reset, name='password_reset'), path('esqueci_senha/enviado/', auth_views.password_reset_done, name='password_reset_done'), re_path(r'^recadastrar_senha/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', auth_views.password_reset_confirm, name='password_reset_confirm'), path('recadastrar_senha/', auth_views.password_reset_confirm, name='password_reset_confirm'), path('recadastrar_senha/pronto', auth_views.password_reset_complete, name='password_reset_complete'), But the e-mail is never sent. I'm pretty sure the configs are right, because the confirmation account e-mail works: EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = 'email@gmail.com' EMAIL_HOST_PASSWORD = 'teste@1234password' EMAIL_PORT = 587 EMAIL_USE_TLS = True DEFAULT_FROM_EMAIL = 'Teste <email@gmail.com>' BASE_URL = '127.0.0.1:8000' And it goes to the right URL esqueci_senha/enviado/ But I don't receive the e-mail. The user is a valid one. I can login with its withou problem. Any idea ofabout the problem? -
Django - NOT NULL constraint failed and WSGIRequest error
I'm making a store type site with Django and currently working on a review and rating system. My 'reviews' app includes a Model called 'Review' and a view for adding a review from the product description template. The model: class Review(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE) author = models.ForeignKey('auth.User', on_delete=models.CASCADE) title = models.CharField(max_length=100) content = models.TextField() rating = models.IntegerField(blank=True, null=True) created_date = models.DateTimeField(auto_now_add=True) The problem I'm having is that the view isn't passing the product_id field, which results in a NOT NULL constraint failed error. That view is as below: @login_required def add_review(request): if request.user: if request.method == "POST": review_form = ReviewForm(request.POST) if review_form.is_valid(): review = review_form.save(commit=False) review.author = request.user review_form.save() messages.success(request, "Review posted successfully!") return redirect(reverse(get_reviews)) else: review_form = ReviewForm() else: messages.error(request, "You must be a member to post reviews.") return redirect(reverse(get_reviews)) args = {'review_form': review_form} return render(request, "add_review.html", args) However, when I try and rectify that by using the same method to pass the author (which I know works as I've used that code elsewhere), I get a WSGIRequest error stating that "object has no attribute 'product' ". The added line seen below: ... if review_form.is_valid(): review = review_form.save(commit=False) review.product = request.product review.author = request.user review_form.save() ... As may … -
Django Deplaying to AWS EC2 (from github)
Recently I have began to use AWS, I've built a python / django app, and now I want to deploy it onto AWS EC2 This is the first time I have tried to deplay a Django application, so please bear with me, anyway I started with the AWS docs to try and get a app up and running, but found it to be not in depth enough. I found this Youtube tutorial - https://www.youtube.com/watch?v=QjrfUO91wfc which does cover all what I want to do, pull files off of github onto EC2 and deploy. All was going well until sudo systemctl daemon-reload sudo systemctl start gunicorn sudo systemctl enable gunicorn sudo vim /etc/nginx/sites-available/python-django-product-review/prodreview For some reason, directories haven't been created within site-available, the only file I have in there is default. I have tried to create said files and directories but permissions seems to be a issue too Here is my github repo - https://github.com/C7LF/python-django-product-review Does anyone have any advice? or a simpler way of uploading my app? Thanks. -
Django custom user model gives error: TypeError: create_superuser() got an unexpected keyword argument 'username'
I'm creating a Django custom user based on a tutorial as below: class UserProfileManager(BaseUserManager): """Helps Django work with our custom user model.""" def create_user(self, email, name, password=None): """Creates a user profile object.""" if not email: raise ValueError('Users must have an email address.') email = self.normalize_email(email) user = self.model(email=email, username=name) user.user_id = -1 user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, name, password): """Creates and saves a new superuser with given details.""" user = self.create_user(email = email, name = name, password = password) user.is_superuser = True user.save(using=self._db) class CustomUser(AbstractBaseUser, PermissionsMixin): """Represents a user profile inside our system""" email = models.EmailField(max_length=255, unique=True) username = models.CharField(max_length=255, unique=True) user_id = models.IntegerField() objects = UserProfileManager() USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['email'] def __str__(self): return self.email And I get the following error when I try to create a superuser: TypeError: create_superuser() got an unexpected keyword argument 'username' However if I change the name of "username" field to "name", I will be able to create a superuser with no error! Does anyone know why can't I name the field anything other than "name"? -
Query string compare fails in django views. Why?
I have a filter system that lets users filter out the games they've played by a few parameters that are stored as fields on the models. I'm passing in the filter as a query string from my template like this: <li> <a href="{% url 'games_filter' %}?filter='my_active_games'&user={{user.email}}">My Active Games</a> </li> And in my views, I'm doing this: def games_filter(request): objects_filter = str(request.GET.get('filter')) user = User.objects.get(email=request.GET.get('user')) if str(objects_filter) == 'my_active_games': raise Exception('Line 56') games = Game.objects.filter(Q(owner=user) & Q(status='New') or Q(status='Qualify')) elif str(objects_filter) == 'all_active_games': games = Game.objects.filter(Q(status="New") | Q(status="Qualify")) elif objects_filter == 'my_played': games = Game.objects.filter(Q(status="Played") & Q(owner=user)) elif objects_filter == 'my_finished': games = Game.objects.filter(Q(status="Finished") & Q(owner=user)) elif objects_filter == 'my_deleted': games = Game.objects.filter(Q(status="Deleted") & Q(owner=user)) else: games = Game.objects.all() But the function won't enter the first if condition, skips directly to the else part. I have tried: Converting objects_filter to string by doing str(objects_filter) Using objects_filter.encode('utf8') Converting the request.GET.get('filter') to a string Neither of these worked. What am I doing wrong? -
Django Rest Framework: One URL for POST, one URL for GET on the same APIView
I am currently writing a POST request to create an A record: client.post(url, {'name' : 'foo', 'another_field' : 2}) And on the server side, on the view, I have: class AQuery(generics.GenericAPIView): queryset = A.objects.all() serializer_class = ASerializer def get(self, request, param1, param2, format=None): ... # do some special query based off of param1 and 2. def post(self, request, format=None): serializer = self.get_serializer(data=request.data, many=False) if serializer.is_valid(): serializer.save() return Response(...) return Response(...) As you can see, I want param1 and param2 to be captured in the url, and so in the urls.py: urlpatterns = [ # for GET path('A/<str:param1>/<path:param2>', views.AQuery.as_view(), name='A query GET') # for POST path('A', views.AQuery.as_view(), name='A query POST') ... ] Obviously, I can add the param1 and param2 to the post function def, but they would just be there, with no purpose. Seems like bad code smell to me. How would I implement something like this, and explicitly disallow the client to make a POST request to A/param1/param2, and explicitly disallow the client to make a GET request to A/? -
Django rest easy way to decrease field value
I am using Django Rest and I need to decrease the value of an integer field by a certain quantity. Right now I am first getting the value currently stored, then calculating the new value, and finally writing the new value back. Is there any way to do this in a simpler way? -
In Django what is the method of a `TextField` / `Field` object that returns its content?
In Django when needing to display the content of an object of type django.db.models.fields.TextField in a template, if this object is denoted as textfield, we can display it with the simple {{ textfield }} command. However, while exploring the code of django.db.models.fields.TextField, I did not find any method returning the content of the TextField. Indeed, while checking django.db.models.fields.TextField and its super class django.db.models.fields.Field I did not find any method that do the job. Thus, is there such a method returning the content of a TextField or even a Field, else what is the machinery used here to return it ? -
IntegrityError after table restore from backup, PostgreSQL + Django
I am trying to restore a table in my database from some db backups. The backup is created via pgAdmin 4 UI tool, select a table and next backup it. And here is the command that pgAdmin executes: --file "C:\\Users\\cchie\\DOCUME~1\\DB_BAC~1\\CURREN~1" --host "localhost" --port "5432" --username "postgres" --no-password --verbose --format=c --blobs --table "public.cosmetic_crawler_currency" "last_cosmetics" The problem is that I have an AutoField in that table and when I restore a table, and next try to add new items into it - I have errors like this: django.db.utils.IntegrityError: duplicate key value violates unique constraint "cosmetic_crawler_product_pkey" DETAIL: Key (product_id)=(27) already exists. The above exception was the direct cause of the following exception: psycopg2.IntegrityError: duplicate key value violates unique constraint "cosmetic_crawler_product_pkey" DETAIL: Key (product_id)=(27) already exists. As I understand - that means that PostgreSQL server does not know that I have restored the AutoField counter and tries to count from 0? Is there a way to fix that? P.S. Restoring process is the same as backuping - manually in pgAdmin UI. -
How to organize caching using additional Database Django
I have django app with database which stores list of my friends in database: their names, addresses, facebook links etc. class Friend(models.Model): name = models.CharField() address = models.CharField() fb_link = models.CharField(blank=True) #etc I also have another app which logic is to generate and print web-page with latest photos of my friends on facebook using their fb_link. As long as I have lots of friends, I need to do about 300 requests to facebook API which takes around a minute, so I can't do it every time somebody loads a page. So I decided to do some sort of haching: I'll use cron to call photos_reload function once per 5 minutes, which would do all API requests and recalculate latest photos. I also need to store it in some sort of database. I created model for it: class FriendLink(models.Model): fb_link=models.charField() latest_photo=models.charField() The problem is I can change my friendslist: add new friends, remove old friends, change friend's fb_page. So here's the question: how to organize second database to follow first database format and keep latest photos of my friends, without changing my first app? -
Django 500 server error only in one URL
I have a Django project in production and when I try to access /calendar endpoint it shows me a 500 error. In local development all the urls work fine, and in production too except this one. Where and how can I check why this endpoint throws an 500 error? -
Django static files doesn't load on deployed server
I use last versions of django, apache and mod_wsgi. I tried to deploy project on Ubuntu 16. The problem related to static files. They didn't load as shown in the figure below (figure there). Actually, maybe I just don't see where I made a mistake, because I tried most things in the google and stackoverflow. Code in the .conf file: Alias /media/ /var/www/fpbg/media Alias /static/ /var/www/fpbg/static <Directory /var/www/fpbg/media> Require all granted </Directory> <Directory /var/www/fpbg/static> # also I tried staticfiles Require all granted </Directory> Code in the settings.py: STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), # '/var/www/static/', ] I executed manage.py collectstatic, It seems, I do everything, but it still doesn't work... HELP! -
What is the best way to deal with NoSQL databases in django?
I know it's somewhat a general question. I'm pretty new to django and whilst getting my first experience with it I faced a few problems with making it play nice with MongoDB. I would like to know what django developers normally use when they have to deal with non relational databases like MongoDB. -
reading data in views from a file placed in static folder in django
from django.contrib.staticfiles.templatetags.staticfiles import static path = static('pickle/Classifier.txt') with open(path , 'rb') as fl: classifier = pickle.load(fl) gives an error (no such file or directory /static/pickle/Classifier.txt) -
Adjusting an external Django App with my project
I have downloaded and installed this "Reservation" app to my project successfully http://djreservation.readthedocs.io But I'm encountering a problem in adjusting it to my main model. The website I'm working on is working on letting people who own a car and have a trip to propose empty seats for passengers who have the same itinerary. My main app I created consists of a form that the user would fill to create an ad the form contains a field with the number of seats The app I downloaded is for reservation but I don't know how to use it in views and forms I want the users to reserve a seat and the ad creator accept or deny it. I'm trying to understand this code: from djreservation.views import SimpleProductReservation from .models import MyModel class RoomReservation(SimpleProductReservation): base_model = MyModel # required amount_field = 'quantity' # required max_amount_field = 'max_amount' # required extra_display_field = [] # not required -
Decrypting SAML2 response using pysaml2 Python module
I am integrating my app with okta to have single sign on. Okta will be passing some user information in SAML response which I need to use in my application. Hence, we decided to encrypt the saml response(xml) at IDP using my server(apache) public key. Now I am trying to decrypt the saml2 response so that I can get the attributes. My applications uses Python 3.5 Django 1.11 pysaml2 python module I am using below to validate/parse the saml2 response coming from okta https://github.com/fangli/django-saml2-auth If the saml response is not encrypted, I am able to process the response and able to get the user identity and user attributes from it. However once it is encrypted at okta end with my server public key, I am not able to decrypt with my private key. The saml setting I have at my application is below : saml_settings = { 'metadata': { "local": [ metadat_xml ], }, 'service': { 'sp': { 'endpoints': { 'assertion_consumer_service': [ (acs_url, BINDING_HTTP_REDIRECT), (acs_url, BINDING_HTTP_POST), (https_acs_url, BINDING_HTTP_REDIRECT), (https_acs_url, BINDING_HTTP_POST) ], }, 'allow_unsolicited': True, 'authn_requests_signed': False, 'logout_requests_signed': True, 'want_assertions_signed': True, 'want_response_signed': False, }, }, 'key_file': "mykey.key", # private part 'cert_file': "mykey.crt", # public part 'xmlsec_binary': '/usr/bin/xmlsec1', 'encryption_keypairs': [{ 'key_file': 'mykey.key', … -
In Django, is there an implicit way to add key-value pairs to request.COOKIES?
I'm working on a project which uses HttpRequest.COOKIES. The project contains ListViews and UpdateViews, and also contains filter forms in the ListView which work in a way similar to django-filter, by appending filter parameters to the querystring. The behavior I'm trying to explain is that when you click 'Save & Exit' in an UpdateView for which the success_url is the corresponding ListView, you get back to the list view with the same filters applied. This works with the request.COOKIES; for example, if I drop into the debugger from the get method, I see ipdb> urlparse(request.META.get('HTTP_REFERER')).path '/dashboard/families/24/case-management' ipdb> request.COOKIES {'filters': 'q=&guide=6&next_outreach=&vip=&app=', 'csrftoken': 'eutePkGikmrb2MwHktAse43ZSODIv542sz7n7aCAfRXHWuJdbUAkP39BwNDdKidL', 'messages': '1e3a74a5e42ed75d062de51a8688f6a123453471$[["__json_message",0,25,"Family was updated successfully."]]', 'sessionid': '.eJxVjs0OgjAQhN-lZ1P5kyJH7z5Ds7QLVJvWtAWMhHe3CAe5THb229nMTCb1ASe5tp0yfFQ4kXomPuCL1GbQ-vSbuYQAESy7bZVGv3l8Bwd_fAStokPJD8FIOAyh54NHx5UkNSmqlBy2DYgnmhXJB5jOUmFNcKqh6wndqad3K1Hf9tvDgx58H9NpzhJsMMO8LBgIdm3ZJc9kBYmAa1kxkcgSMK1i2IZYEUclcOu0-hCFrrKBc0qWLyNFZy8:1fDa7H:6TgjBOh6WG347nfLuLAtt0j4gsE'} ipdb> Here, the HTTP_REFERER is the detail view, and there are filters in the COOKIES. The ListView has its get() method overridden as follows: def get(self, request, *args, **kwargs): if should_delete_filter_cookie(request, path=reverse('dashboard:families')): request.COOKIES.pop('filters', None) self.query_dict = query_dict_from_params_or_cookies(request) return super().get(request, *args, **kwargs) where the helper methods are defined as follows: def query_dict_from_params_or_cookies(request): query_dict = request.GET cookie_filters = request.COOKIES.get('filters') if cookie_filters and not list(query_dict.items()): query_dict = QueryDict(cookie_filters) return query_dict def should_delete_filter_cookie(request, path): referer_path = urlparse(request.META.get('HTTP_REFERER')).path if referer_path and path not in referer_path: return True else: return False What I don't understand is at … -
Chart JS title passed from Django - issue with apostrophe '
I am using chart.js to render data passed from a Django view. I have various types of charts all working well, EXCEPT when my chart title includes as apostrophe. For example, the title: My child's learning gets displayed as: My child&#39s learning The code below shows how I use the title which is the variable chart_row.heading_2 passed from the Django view options: { title: { display: true, text: '{{ chart_row.heading_2 }}' } } The title works in all other instances where there is no apostrophe. Is there any workaround to this? -
Convert the value of a field in a Django RawQueryset to a different field type
I'm using a library called django_smalluuid that converts UUIDFields to smaller UUIDs. I use these in my URLs and templates to keep links shorter than they'd be with a full uuid. I need to write a very complex query that does a wide array of joins that returns a postgres UUIDField that I'd like to return as a SmallUUID. I'm using .annotate() with Value()s inside to pull the fields the query returns that are not part of the model. I have some code that looks something like this (this is significantly clarified): query = "SELECT othertable.uuid_field as my_uuid FROM myapp_mymodel JOIN othertable o ON myapp_mymodel.x = othertable.x" MyModel.objects.annotate( my_uuid=models.Value('my_uuid', output_field=SmallUUIDField()), ).raw(query) But there's a problem, output_field=SmallUUIDField() seems to be completely ignored by django when it compiles the RawQueryset result from this code. my_uuid is always returned as the type of the field it gets back from Postgres (because it's a native Postgres UUIDfield it returns as a uuid.UUID) If I do SELECT cast othertable.uuid_field as text) it will return a string. I'd very much like that field to return as a SmallUUIDField. If it's in that format I can pass the RawQueryset straight into my templates and access the smaller … -
python django: avoid file overwrites with collectstatic
When I use Django's "manage.py collectstatic" command all static files from the apps are stored in one folder (STATIC_ROOT). Now several apps have a folder app1/static/css, app2/static/css, etc. They all get merged into one folder static/css. That causes some files to overwrite if they have the same name. Is there a good practice to avoid this problem? -
What is the most pythonic way to pass kwargs in this function?
I want to create a shortcut function for creating a question object in my django tests. I don't like to type something like new_q(timedelta(minutes=1)) all the time and I want to write that timedelta only once and be able to pass it's arguments in a nice way to the new_q function. Is there any pythonic one line solution to this in which I don't have to capture the kwargs to a variable and then create a timedelta object in a new line? from datetime import timedelta from django.utils import timezone def new_q(question_text = '', offset=timedelta(**kwargs)): time = timezone.now() + offset return Question.objects.create(question_text=question_text, pub_date=time) usage: test1 = new_q(minutes=1) test2 = new_q('yo?', seconds=1) test2 = new_q(question_text = 'maw?', hours=11) test2 = new_q(question_text = 'maw?') test2 = new_q('yo?') -
Django AWS using gunicorn 'No module named django.core.wsgi'
I'm having issues deploying my Django app on AWS with gunicorn. I have created my virtualenv, along with installing Django etc. However when I do gunicorn --bind 0.0.0.0:8000 prodreview.wsgi:applicationI get the error: ImportError: No module named django.core.wsgi I am running the command in the same directory as 'wsgi' Am I missing something?