Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
I am getting "Cahe file too long header" error in nginix. I am getting around 170 errors in a week. How to fix this?
19#0: *474842 cache file "/tmp/nginx-cache/e/57/3ebab6b2bc878a01f89035efde31657e" has too long header, client: 10.255.0.2, server: ntnservices.buzztime.com, request: "GET /abc/def/?someid=1&someid=1000 HTTP/1.1", -
Call Django template tag with userinput
I want to use a Django custom template tag for checking whether the user id is in the database or not. We already created a custom template tag which works so far, but I can't get it to work with the form. Goal is to not refresh the entire page but just show the result of this query. How is it possible to achieve this? HTML Form: <form role="form" method="post" enctype="multipart/form-data"> {% csrf_token %} <input type="text"> <button>Check Me</button> </form> {{ test|testid:123 }} <!-- Works, shows True or False --> Filter: @register.filter def testid(value, test_id): if Test.objects.filter(id=test_id).exists(): return True return False I tried to look this up, but it seems that most of the people do this by implementing iQuery, just using forms or something similar and I'm trying to avoid this and wanted to use the template tag for this. -
Best practices for periodic Django data ingestion and modeling
I have a CSV file with ~1 million entries I want to model and import into my Django DB. The schema looks something like: id, name, address, num_employees. Every few weeks, the CSV file gets updated - entries get added and deleted, but most stay the same. And rarely the schema changes slightly. I've read about things like star-schemas, data lakes, ingestion pipelines, data adapters...but there are SO MANY variations and ideas. Some seem overkill, some seem too naive. I'm having trouble understanding where to start. Any best practices or patterns I should follow for this? Btw, some things I'm interested in: Query for the most up to date data. Perform some sanity checks and handle things like "keep the old address if it's present in an old entry, but absent in the current version." Be able to audit history and manually correct entries that are discovered to be invalid. Handle rare schema changes. Maybe next year address is split into street_address, city, state, zip_code. How can I keep this sane? Ingest large files (10-100mb) without holding up my server or bogging down my DB when updating. -
how to handle and suppress the ctrl+q warning works dynamically during the response to the html page
commands: Use arrow keys to move, '?' for help to quit, '<-' to go back. Arrow keys: Up and Down. Right to follow a link; Left to go back. H)elp O)ptions P)rint G)o M)aninscreen Q)uit /=search [delete]=history list -
Django allauth not accessing static files in production
I run my Django app on my laptop using python manage.py runserver and my allauth pages render fine. When I run them on production with python manage.py runserver 0.0.0.0:8000 they break. I am pretty sure the problem is with accessing the static ccs files (both bootstrap and my own css one). settings.py import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = 'hidden' DEBUG = True ALLOWED_HOSTS = ['18.207.218.217', '127.0.0.1'] INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', 'streaming_platform', 'users', 'widget_tweaks', 'allauth', 'allauth.account', 'allauth.socialaccount', ] SITE_ID = 1 ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_USERNAME_REQUIRED = True ACCOUNT_AUTHENTICATION_METHOD = 'email' EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.SignupForm' MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'mainsite.urls' AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', 'allauth.account.auth_backends.AuthenticationBackend', ) TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ["templates"], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'django.template.context_processors.request', ], }, }, ] WSGI_APPLICATION = 'mainsite.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True STATIC_URL = '/static/' STATICFILES_DIRS = ( … -
Using ForeignKey to sort with order_by and distinct not working
views.py 'recent_games': Game.objects.all().order_by('title', '-update__date_published').distinct('title')[:5], models.py class Game(models.Model): title = models.CharField(max_length=100) slug = models.SlugField(unique=True) description = models.TextField() date_published = models.DateTimeField(default=timezone.now) cover = models.ImageField(upload_to='game_covers') cover_display = models.ImageField(default='default.png', upload_to='game_displays') developer = models.CharField(max_length=100) twitter = models.CharField(max_length=50, default='') reddit = models.CharField(max_length=50, default='') platform = models.ManyToManyField(Platform) def __str__(self): return self.title class Update(models.Model): author = models.ForeignKey(User, models.SET_NULL, blank=True, null=True,) # If user is deleted keep all updates by said user article_title = models.CharField(max_length=100, help_text="Use format: Release Notes for MM/DD/YYYY") content = models.TextField(help_text="Try to stick with a central theme for your game. Bullet points is the preferred method of posting updates.") date_published = models.DateTimeField(db_index=True, default=timezone.now, help_text="Use date of update not current time") game = models.ForeignKey(Game, on_delete=models.CASCADE) article_image = models.ImageField(default='/media/default.png', upload_to='article_pics', help_text="") platform = ChainedManyToManyField( Platform, horizontal=True, chained_field="game", chained_model_field="game", help_text="You must select a game first to autopopulate this field. You can select multiple platforms using Ctrl & Select (PC) or ⌘ & Select (Mac).") The distinct method on the query works perfectly however the -update__date_published doesn't seem to be triggering. I'm trying to sort games by the games most recent update(post) without returning duplicates. -
how to create django custom filefield to upload to amazon s3 with specfic bucket
I have a few Amazon S3 Bucket, i would like to create multiple file field that save to respective bucket i've read about creating a storage class and use S3Boto3Storage, however this require specification of Bucket name at settings level, i'm not able to set different bucket for different use case, example below: class AmazonS3Storage(S3Boto3Storage): def get_available_name(self, name, max_length=None): return name Anyone have solution to this? -
Duplicated Time Zones Using Python pytz
I am using python 3.6 & django 1.10.5. I have a drop down list of common time zones that a user can select from. I'm also using Babel to translate the time zones to different languages. Regardless of the language, the drop down list sometimes displays 2 or more entries for the same common time zone. Here is a screen shot of an example: I have been advised that the issue may be due to the display of standard time and day light savings time (DST). Here is my code forms.py code: def __init__(self, available_languages, *args, **kwargs): super(UserProfileForm, self).__init__(*args, **kwargs) cur_language = translation.get_language() # This is necessary because Babel 2.4.0 can't handle dashes. A newer version should fix this, once it's released. translated_timezones = [(tz, get_timezone_location(tz, locale='it')) for tz in pytz.common_timezones] translated_timezones.sort(key=lambda tz: tz[1]) self.fields['timezone'].choices = translated_timezones I have read through the docs and read through many SO threads, but I have no solution to limiting the display to a single entry for each common time zone. Does anyone have a solution? -
What is are some best practices to Version python code logic that is independent of versioned API's?
I am aware of API versioning where the API's are versioned with respect to URL's host/api/v1/get_user host/api/v2/get_user How does the versioning of large projects and modules work in python? My project structure looks like this my_project/ --__init__.py |--app/ --__init.py |--logic/ --__init__.py |--v1/ --__init__.py --my_logic.py |--v2/ --__init__.py --my_logic.py |--api/ --__init__.py |--v1/ --__init__.py --my_api.py |--v2/ --__init__.py --my_api.py I know it works but is there a better way to structure a large project with more contributors where API's may use Certain objects from v1 Logic and v2 logic. -
There is no document formatter for 'django-html'-files installed
In vscode, shift+alt+f for auto-formatting does work in HTML, but not work for Django template file. How can I use autoformatting function in VS code? -
Elastic Beanstalk Django Database Configuration
I'm running into a situation where my django application cannot connect to the postgres instance that have both been spun up by the same EBS environment. I've confirmed that the security groups that both the RDS and EC2 instances in are setup to communicate. I confirmed the credentials for the database are correct as i am able to use the CLI for postgres to log in both locally, as well as while ssh'd into the EC2 instance. Third, and weirdest of the symptoms, is that the migrations work. So while migrating, the application is able to connect to the database and run migrations, but when trying to access those tables via the views.py files, i get the dreaded password authentication failed error. I'm totally stuck, and have been trying everything i can think of for the last few hours. Any help i can get would be a huge huge help. Thanks! -
Django channels pytest testing. django.core.exceptions.ImproperlyConfigured. .
I am getting this error when running pytest. I am following this tutorial: https://channels.readthedocs.io/en/latest/topics/testing.html django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. The following the things I've tried: place django.conf.settings.configure() at the top of the test script place the following code at the top of the test script ROOT_DIR = environ.Path(file) - 2 env = environ.Env() env_file = str(ROOT_DIR.path('.env')) env.read_env(env_file) -
error when creating a virtual environment under linux Ubuntu
I entered 'mkvietualenv ssccenv' at the terminal and reported an error. 'OSError: Command /root/.virtualenvs/ssccenv/bin/python2 - setuptools pkg_resources pip wheel failed with error code 2' can you tell me why is that- -
Django: importing a model works, importing an instance of it immediately afterwards fails
I'm working with this repo: https://github.com/IntelligentTrading/data For reference, the latter of these two lines is the offending one, I believe: from apps.channel.models import ExchangeData from apps.channel.models.exchange_data import POLONIEX The page has instructions. I've almost gotten to the end, then I run this: python manage.py poloniex_polling For reference, here's the code for poloniex_polling.py: import json import logging import schedule import time from django.core.management.base import BaseCommand from requests import get, RequestException from apps.channel.models import ExchangeData from apps.channel.models.exchange_data import POLONIEX logger = logging.getLogger(__name__) class Command(BaseCommand): help = "Polls data from Poloniex every 1 minute" def handle(self, *args, **options): logger.info("Getting ready to poll Poloniex...") schedule.every(1).minutes.do(poll_latest_poloniex_data) keep_going=True while keep_going: try: schedule.run_pending() time.sleep(1) except Exception as e: logger.debug(str(e)) logger.info("Poloniex polling shut down.") keep_going = False def poll_latest_poloniex_data(): try: logger.info("polling for Poloniex data...") req = get('https://poloniex.com/public?command=returnTicker') poloniex_data_point = ExchangeData.objects.create( source=POLONIEX, data=req.json(), # the exact json from the request data timestamp=time.time() # now ) logger.info("Saving Poloniex data...") except RequestException: return 'Error to collect data from Poloniex' When I run python manage.py poloniex_polling , I get: INFO:settings:Deployment environment detected: LOCAL INFO:settings:Importing vendor_services_settings INFO:settings:LOCAL environment detected. Importing local_settings.py Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "/home/cerulean/.virtualenvs/ITF/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "/home/cerulean/.virtualenvs/ITF/lib/python3.6/site-packages/django/core/management/__init__.py", line … -
Reverse for 'plane_detail_book' with keyword arguments '{'pk': 2}' not found 1 pattern(s) tried: ['<int:\\ pk>/$']
I am trying to make airline search from a certain source to destination and after listing the planes i want the planes to be links so that clicking on them takes me to the plane detail page but some reason this error keeps popping. I am new to this so cant seem to figure out the problem. Any help will be appreciated. views.py def index(request): form = SearchForm() if request.method == "POST": form = SearchForm(request.POST) if form.is_valid(): return plane_list(request) else: print("ERROR") return render(request,'index.html',{'form':form}) def plane_list(request): form = SearchForm(request.POST or None) if request.method == "POST": form = SearchForm(request.POST) if form.is_valid(): p = Route.objects.filter(route_dest = form.cleaned_data.get('destination'),route_src = form.cleaned_data.get('source')) if not p: route_id = 1000 else: route_id = p[0].route_no flights = FlightDetail.objects.filter(route=route_id) return render(request, 'plane_list.html', {'form': form,'flights': flights}) def plane_detail_book(request): flights = FlightDetail.objects.all() return render(request, 'flightdetail.html',{'flights': flights}) models.py class Route(models.Model): route_no = models.CharField(max_length = 10) route_dest = models.CharField(max_length = 100) route_src = models.CharField(max_length = 100) class FlightDetail(models.Model): flight_no = models.CharField(max_length = 100,) route = models.CharField(max_length = 100) def get_absolute_url(self): return reverse("flight_detail_book",kwargs={'pk': self.pk}) template <body> {% if flights %} {% for flights in flights %} <div class="container"> <a href="{% url 'plane_detail_book' pk=flights.pk %}">{{ flights.flight_no }}</a> </div> {% endfor %} {% else %} <h1>NO PLANES</h1> … -
How django rest framework validate list of object?
I already know that drf has an interface to add a validator to obejct, I have a view that can create multiple Param objects with one request, I want to verify if there are two or more objects whose name and value fields are the same, How can I validate the list of object in serializer? models.py class Param(models.Model): name = models.CharField(max_length=256) value = models.CharField(max_length=256) class Meta: unique_together = ('name', 'value') serializers.py class ParamSerializer(serializers.ModelSerializer): class Meta: model = models.ParamKey fields = ['name', 'value'] extra_kwargs = { 'id': { 'required': False, }, 'name': { 'required': False, } } views.py class ParamViewSet(viewsets.ViewSet): def create(self, request): serializer = serializer.ParamKeySerializer( data=request.data, many=True) if serializer.is_valid(): return Response() return Response() -
Django slug url in perisan 404
I have a django url: path('question/<slug:question_slug>/add_vote/', views.AddVoteQuestionView.as_view()) It work fine with english slug but when slug is persian something like this: /question/سوال-تست/add_vote/ django url throw 404 Not Found, is there any solution to catch this perisan slug url? -
Django - mongoengine - Circular imports in models files of different apps
I have 2 Apps, user and boxes. Their models contain 2 classes each and none of the classes have circular dependency but model file have circular dependency. Their models are as below #users/models.py from mongoengine import fields, Document from boxes.models import * class Token(Document): id = fields.IntField(primary_key=True) key = fields.StringField(required=True) box = fields.ReferenceField(Box) class User(Document): id = fields.IntField(primary_key=True) name = fields.StringField(required=True) #boxes/models.py from mongoengine import fields, Document from users.models import * class Box(Document): id = fields.IntField(primary_key=True) name = fields.StringField(required=True) class Testing(Document): id = fields.IntField(primary_key=True) field1 = fields.StringField(required=True) field_ref = fields.ReferenceField(User) I am getting below error in Testing Class during starting the server NameError: name 'User' is not defined Things I tried : #Trial 1 from django.apps import apps field_ref = fields.ReferenceField(apps.get_model('users', 'User')) #error django.core.exceptions.AppRegistryNotReady: Models are not loaded yet. #Trial 2 field_ref = fields.ReferenceField('User') #Error mongoengine.errors.ValidationError: ValidationError (Box:5b8ddef7a095d8586d2263bb) (A ReferenceField only accepts DBRef or documents: ['user']) I tried solutions given in other posts as well but the are not working. I am using Django =1.9. Any help will be really appreciated. -
using generic createapiview and updateapiview in the same view in django restframework
Iam trying to find out if it is possible to post and patch in the same view using the generic api method in Django rest framework. I dont think there is any generic class that allows create and update altogether, can anyone tell me if the below configuration would allow me to use post and patch method in the same view. class QuestionList(generics.updateAPIView, generics.CreateAPIView): queryset = Question.objects.all() serializer_class = QuestionSerializer -
How to run Django channels test?
I am following the tutorial here https://channels.readthedocs.io/en/latest/topics/testing.html It's telling me how to write a test, but it's not telling me how to run it. -
Django 1.11 - Query to exclude based on other table
Problem I'm trying to build a system to invite people to teams so I have Team and Invitation models. I'm trying to query users that don't have a team and that aren't in the Invitations table with that team. So far I can filter the users that don't have a team but I can't figure out how to exclude users that already have an invite from that team. What I have so far from django.contrib.postgres.search import SearchVector sv = SearchVector('first_name', 'last_name', 'email') team = parsed_body['team_id'] query = parsed_body['query'] users = User.objects.annotate(search=sv).filter(search=query, profile__team=None) Team Model from django.db import models class Team(models.Model): name = models.CharField(max_length=32, blank=False, default=None) description = models.TextField(max_length=512, blank=False, default=None) created_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) Invitation Model from django.db import models class Invitation(models.Model): team = models.ForeignKey(Team, on_delete=models.CASCADE) shifter = models.ForeignKey(User, on_delete=models.CASCADE) -
table merge cells by angularjs or django templete language
Here is my expectation I wrote a demo in codepen, although I figured out the rules, but I can't do it with angularjs or django. https://codepen.io/scheinin/pen/LaQpRV Rendered data(Complete data in codepen ) {"NFL": {}, "AFL": { "teams": [ { "name": "New England Patriots", "area": "Foxborough", "position": [ { "name": "QB", "player": [ { "name": "Tom Brady", "rings": "6" } ] }, { "name": "TE", "player": [ { "name": "Rob Gronkowski", "rings": "3" } ] } ] } ] }} The table is one-dimensional, but the data is multidimensional. -
Can django webapp and django-rest-framework live together?
I have a Django WebApp to administrate certain things of my business. It's currently using Django 1.5.5, but I just migrated the code to Django 1.11. The thing is that we are developing another apps using other technologies, and due to all my information is in the Django app, I decided to add Django Rest Framework to my existing Django Webapp. All ok so far, beautiful, API with Token access... happy... But, then I realized that on PROD env, I've ALLOWED_HOST setting. :(. I added that line in my devbox, and the happiness end. I tried adding CORS support using django-cors-headers, but, so far, I've not successful. So, to avoid wasting time, I want to ask to people that knows more than me about it, if the Django App and the DRF API can live together, without removing the ALLOWED_HOST setting or setting it as ALLOWED_HOSTS = ['*']. Thanks in advance! -
Django rest framework serializer create method claims wrong number of arguments
I'm using nested django serializers: class PhaseSerializer(serializers.ModelSerializer): features = FeatureSerializer(many=True) class Meta: model = Phase exclude = ["project"] depth = 3 def create(self, validated_data): features_data = validated_data.pop("features") phase = Phase.objects.create(**validated_data) return phase class ProjectSerializer(serializers.ModelSerializer): phases = PhaseSerializer(many=True) class Meta: model = Project fields = "__all__" depth = 3 def create(self, validated_data): phases_data = validated_data.pop("phases") project = Project.objects.create(**validated_data) for phase_data in phases_data: phase_data.update({"project": project}) PhaseSerializer.create(phase_data) return project For some reason calling the PhaseSerializer.create method complains that create() missing 1 required positional argument: 'validated_data'. I don't see how this can be the case, as it is called with one argument (definitely not null...) -
Django: Gets SMTPHeloError when sending password reset email to Outlook account with @hotmail.com address
I'm trying to get the password reset capabilities to work in my Django app. However, I'm running into an error when I try to get an email sent to my Outlook account when my email has the @hotmail extension. (Remember hotmail when defunct years ago but the @hotmail became compatible with Outlook.) Here's the configuration in the settings.py: EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp-mail.outlook.com' \\I tried that and 'smtp.live.com' and did not work either EMAIL_HOST_USER = 'my username' EMAIL_HOST_PASSWORD = 'my password' EMAIL_PORT = 587 EMAIL_USE_TLS = True Here is the urls.py: from django.contrib import admin from django.contrib.auth import views as auth_views from django.urls import path, include from users import views as user_views from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('admin/', admin.site.urls), path('register/', user_views.register, name="register"), path('profile/', user_views.profile, name="profile"), path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'), path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'), path('password-reset/', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'), name='password_reset'), path('password-reset/done/', auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'), name='password_reset_done'), path('password-reset-confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'), name='password_reset_confirm'), path('', include('blog.urls')), ] if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) Here is the reset_password.html in which has the request password form: {% extends 'blog/base.html' %} {% load crispy_forms_tags %} {% block content %} <div class="content-section"> <form method="POST"> {% csrf_token %} <fieldset class="form-group"> <legend class="border-bottom mb-4">Reset Password</legend> {{ form|crispy }} </fieldset> <div class="form-group"> <button class="btn …