Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Managing helper/utils functions and their sensitive data in django
I am integrating some 3rd party API (say for sending OTP to mobile phones), and I want my system to be FLEXIBLE but SECURE at the same time, consider: Lets suppose right now I am using a vendor named msg91.com for sending SMS/OTP to customers. But in future I might switch to some other vendor (say way2sms.com) for the same service owing to various reasons (say cost/testing/production). I have some helper functions for integrating them consider the pseudo code: if SMS_VENDOR == 'MSG91': msg91_helper_function(): # do some processing specific to vendor # authenticate with msg91 # send SMS/OTP # ------------------------------------------------------------------ elif SMS_VENDOR == 'WAY2SMS': way2sms_helper_function(): # do some processing specific to vendor # authenticate with way2sms # send SMS/OTP I know usually people have a utils.py or helpers.py file in the same django app (usually named common) but the issue is: Each vendor has its own some sensitive data like auth_token that I can't store directly in the helper functions (as code will be shared) What I want: How to I securely store these auth_token like sensitive CONSTANTS while keeping my system flexible First Approach Storing them in .env file (then adding it to .gitignore) and then fetching them as: … -
Django creates Static Folder
When uploading my statics with `collectstatic' on Google Cloud Storage, it does upload the files into the main root of the bucket, instead in a folder called "/static/", so the web on production can't read the statics. How can I create the folder "/static/" and upload the files there on GCS? This are my settings: ALLOWED_HOSTS = ["*"] DEBUG = True INSTALLED_APPS += ["django_extensions", ] DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage" STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage" GS_BUCKET_NAME = "static-bucket" GS_MEDIA_BUCKET_NAME = "bucket-name" -
showing ManytoManyField element in a query loop
So I want to show elements in a loop from a class I created, however I don't know how to call the ManyToMany element out, can you help me? class Tag(models.Model): nametag = models.CharField(max_length=200, null=True) class OA(models.Model): tags = models.ManyToManyField(Tag) ... My function: def home(request): objetos = OA.objects.all() return render(request, {'objetos': objetos}) The problem: {% for i in objetos %} ... <tr>{{i.tags.nametag}}</tr> {% endfor %} In this case 'nametag' already has a value so it's not empty. I tried a few things but wasn't able to do much, I need help please. -
Using django models need to update the dropdown dynamically
I am currently using dependent dropdown in Django and updating the dropdown menu content using two models. This works fine for me. But the requirement in my app is now to fetch the content of dropdown menu dynamically on page load from a third party server (which is source of truth for the data). When I add the content to Django models and then use the webpage, the content is static in nature, as in it is not in sync with the third party server's data. What is the best way to update dropdown menu dynamically from a third party server when using models? -
how to use objects.filter() for filtering a dictionary to a POST method in django rest framework
Models.py class Category(models.Model): name = models.CharField(max_length=50,null=False, blank=False) def __str__(self): return self.name class Photo(models.Model): category = models.ForeignKey(Category, on_delete= models.SET_NULL,related_name='category', null= True, blank= False) image = models.ImageField(null= False, blank = False) description = models.TextField(null=True, blank=True) def __str__(self): return self.description Serializers.py class CategorySerializer(serializers.ModelSerializer): class Meta: model = Category fields = ['name'] class PhotoSerializer(serializers.ModelSerializer): category = CategorySerializer(many=False) class Meta: model = Photo fields = ['id','category','image','description'] view.py from django.http import response from rest_framework import viewsets from rest_framework import serializers from rest_framework.response import Response from rest_framework.serializers import Serializer from rest_framework.views import APIView from .models import Category, Photo from .serializers import PhotoSerializer class DisplayAllViewSet(viewsets.ModelViewSet): queryset = Photo.objects.all() serializer_class = PhotoSerializer class DisplayCategoryViseViewSet(APIView): serializer_class = PhotoSerializer def post(self, request, format=None): data = self.request.data print(data) category = data['category'] print(category) print(category['name']) name=category['name'] queryset = Photo.objects.filter(category=name) serializer = PhotoSerializer(queryset, many=True) return Response(Serializer.data) urls.py from re import I from django.db import router from django.urls import path from django.conf.urls import include from rest_framework import routers from .views import DisplayAllViewSet, DisplayCategoryViseViewSet router = routers.DefaultRouter() router.register('allImages',DisplayAllViewSet) urlpatterns = [ path('', include(router.urls)), path('category',DisplayCategoryViseViewSet.as_view()), ] In Postman : GET request http://127.0.0.1:8000/gallery/allImages [ { "id": 1, "category": { "name": "Sirsi" }, "image": "http://127.0.0.1:8000/sunset.jpg", "description": "Sunset view point" }, { "id": 2, "category": { "name": "Chickmangluru" }, "image": "http://127.0.0.1:8000/tiger.jpg", … -
Dockerize django app along side with cucumber test
Here is the case. I have simple django app with cucumber tests. I dockerized the django app and it works perfect, but I want to dockerize the cucumber test too and run them. Here is my project sturcutre: -cucumber_drf_tests -feature -step_definitions axiosinst.js config.js package.json cucumber.js Dockerfile package-lock.json -project_apps -common docker-compose.yaml Dockerfile manage.py requirements.txt Here is my cucumber_drf_tests/Dockerfile FROM node:12 WORKDIR /app/src COPY package*.json ./ RUN npm install COPY . . EXPOSE 8000 CMD ["yarn", "cucumber-drf"] (this is how I run my test locally) My second Dockerfile FROM python:3.8 ENV PYTHONUNBUFFERED=1 RUN mkdir -p /app/src WORKDIR /app/src COPY requirements.txt /app/src RUN pip install -r requirements.txt COPY . /app/src And my docker-compose file version: "3.8" services: test: build: ./cucumber_drf_tests image: cucumber_test container_name: cucumber_container ports: - 8000:8000 depends_on: - app app: build: . image: app:django container_name: django_rest_container ports: - 8000:8000 volumes: - .:/django #describes a folder that resides on our OS within the container command: > bash -c "python manage.py migrate && python manage.py loaddata ./project_apps/fixtures/dummy_data.json && python manage.py runserver 0.0.0.0:8000" depends_on: - db db: image: postgres container_name: postgres_db volumes: - ./data/db:/var/lib/postgresql/data environment: - POSTGRES_DB=postgres - POSTGRES_USER=bla - POSTGRES_PASSWORD=blaa If I remove I remove test service and run the tests locally everything is … -
Django need first paragraph
I created model with my posts. file blog/models.py: import datetime from ckeditor.fields import RichTextField from django.db import models class Post(models.Model): name = models.CharField(max_length=250) content = RichTextField() date = models.DateField(default=datetime.date.today) and in db i has this record: <p>First paragraph</p> <p>Second paragraph</p> <p>Third paragraph</p> <p>etc</p> <p>...</p> How I can get result First paragraph ONLY -
Installing locales in docker env not working
I'm trying to use locale.setlocale(locale.LC_ALL, 'ar_AE.UTF-8') In Django app, that running in docker environment, with python:3.9.6-slim But it gives me unsupported locale setting error I tried to install locales in Dockerfile using RUN apt-get install -y locales RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \ && sed -i -e 's/# ar_AE.UTF-8 UTF-8/ar_AE.UTF-8 UTF-8/' /etc/locale.gen \ && locale-gen ENV LANG ar_AE.UTF-8 ENV LC_ALL ar_AE.UTF-8 But it didn't fix the problem Anyone know how to solve this please -
Filtering values based on case-when returned values results in a SyntaxError
I have an object snapshot database model, saving some data regarding any arbitrary model in my project: class ObjectSnapshot(Model): user = ForeignKey(to=User, on_delete=SET_NULL, null=True, blank=True) timestamp = DateTimeField(verbose_name=_("Date when the document was generated"), auto_now_add=True) content_type = ForeignKey(ContentType, on_delete=CASCADE) object_id = PositiveIntegerField(verbose_name=_("Id of the referenced object")) content_object = GenericForeignKey("content_type", "object_id") data = JSONField(verbose_name=_("Serialized object"), default=dict, null=False, blank=True) There can be multiple snapshots for the same object (referencing the same object_id and content_type), there are also some snapshots that reference the same "logical object" meaning that e.g. object_id is different but those objects share some field values that denotes that they are the same object (it will be explained better by a query example below). Now I want to write a query retrieving list of those objects, annotated with their "previous" entry. ObjectSnapshot.objects.annotate( previous_id=Subquery( ObjectSnapshot.objects.filter( content_type=OuterRef('content_type'), object_id__in=Case( When( content_type__model='somemodel', then=Subquery( SomeModel.objects.filter( field_name=Subquery( # the `field_name` refers to the same "logical object" SomeModel.objects.filter( id=OuterRef(OuterRef('object_id')), ).values( 'field_name' )[:1] ), ).annotate(ids=ArrayAgg('id')).values('ids')[:1], ), ), default=ArrayAgg(OuterRef('object_id')), output_field=ArrayField(base_field=IntegerField()), ), ).exclude( pk=OuterRef('pk') ).values_list( 'object_id' ).order_by( '-timestamp' )[:1] ) ) Trying to run this query I get: syntax error at or near "ARRAY_AGG" LINE 1: ...g_objectsnapshot"."id")) HAVING U0."object_id" IN ARRAY_AGG(... ^ -
Django models FileField - field.url shows img on screen but gives ```No file``` on download attempts
For an image (or with doing the needed code changes for a different file format), The image appears fine on screen but when I try to download it: Chrome and Edge give Download failed - No file Firefox downloads it when save file is chosen // url -->> django_model_field.url --- model: FileField <a href={url} download="" target="_blank" rel="noreferrer"> <img src={url} alt="img"/> </a> How to fix this, please? Thanks in advance for your help :) -
Change value of field if input is not in choices Django ImportExportModelAdmin
I have this model field as an example: compliance_approve_status = models.CharField(max_length=200, blank=True, null=True, default=None, choices=review_status_choices) with these choices: for_review = 'FOR REVIEW' approved = 'APPROVED' rejected = 'REJECTED' review_status_choices = [(for_review, 'FOR REVIEW'), (approved, 'APPROVED'), (rejected, 'REJECTED')] A simplified version of fields with choices. I want to be able to make a default value in which if a user imports from a file and puts something other than a 'FOR REVIEW', 'APPROVED' or 'REJECTED', or is NULL, it would default to None. I thought adding a default=None would do it, but it accepts other values in Django's ImportExportModelAdmin. -
How to Use Custom Template Tag in Combination With Django's Built-in "with" Tag?
I have this simple tag: myapp/templatetags/my_filters.py @register.simple_tag def get_bookmark_object(content_type, object_id): return Bookmark.objects.get(content_type=content_type, object_id=object_id) In my template, I want to be able to do this: {% load my_filters %} {% with object as bookmark %} {% with bookmark_object=get_bookmark_object bookmark.content_type bookmark.object_id %} {% if bookmark.content_type.model == 'post' %} {% include 'content/post/object.html' with object=bookmark_object user_bookmark=bookmark %} {% elif bookmark.content_type.model == 'note' %} {% include 'content/note/object.html' with object=bookmark_object user_bookmark=bookmark %} {% endif %} {% endwith %} {% endwith %} I get the error: TemplateSyntaxError at /my-page/ 'with' received an invalid token: 'bookmark.content_type' My question is: How do I use my custom get_bookmark_object template tag in a with statement? An example with code would help me clarify a lot. Reference: Django's with built-in -
How to filter a Django queryset, after it is modified in a loop
I have recently been working with Django, and it has been confusing me a lot (although I also like it). The problem I am facing right now is when I am looping, and in the loop modifying the querryset, in the next loop the .filter is not working. So let's take the following simplified example: I have a dictionary that is made from the queryset like this dict = {chicken: 6, cows: 7, fish: 1, sheep: 2} The queryset is called self.animals for key in dict: if dict[key] < 3: remove_animal = max(dict, key=dict.get) remove = self.animals.filter(animal = remove_animal)[-2:] self.animals = self.animals.difference(remove) key[replaced_industry] = key[replaced_industry] - 2 Now the first time it loops (with fish), the .filter does exactly as it should. However, when I loop it a second time (for sheep), the remove = self.animals.filter(animal = remove_animal)[-2:] gives me an output is not in line with animal = filter. When I print the remove in the second loop, it returns a list of all different animals (instead of just 1). After the loops, the dict should look like this: {chicken: 4, cows: 5, fish: 1, sheep: 2} This because first cow will go down 2 and it is the … -
TypeError: Field 'id' expected a number but got <django.db.models.query_utils.query_utils.DeferredAttribute object
Trying to display Trainee(s) only for a spesific NewHireTraining. I am passging additional context data to a training_details view class to show trainees under that training which are related with foreingkey under Trainee's class. It is returning the location of the attribute id instead of the id value. Will really appreciate help models.py class NewHireTraining(models.Model): location = models.ForeignKey(Location, on_delete=models.CASCADE) program = models.ForeignKey(Program, on_delete=models.CASCADE) project = models.ForeignKey(Project, on_delete=models.CASCADE) trainer = models.ForeignKey(Trainer, on_delete=models.RESTRICT) start_date = models.DateField() nesting_date = models.DateField() production_date = models.DateField() requested_headcount = models.IntegerField() starting_headcount = models.IntegerField(null=True, blank=True) ending_headcount = models.IntegerField(null=True, blank=True) end_nesting_headcount = models.IntegerField(null=True, blank=True) def __str__(self): return str(self.program) + ' ' + str(self.project) + ' ' + str(self.start_date) class Trainee(models.Model): first_name = models.CharField(max_length=55) last_name = models.CharField(max_length=55) employee_number = models.IntegerField() hire_date = models.DateField() is_term = models.BooleanField(default=False) new_hire_training = models.ForeignKey(NewHireTraining, on_delete=models.CASCADE, default="") def __str__(self): return self.first_name + ' ' + self.last_name views.py class TrainingDetailsView(DetailView): model = NewHireTraining template_name = 'nht/training_details.html' context_object_name = 'training' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['trainees'] = Trainee.objects.filter(new_hire_training = self.model.id) return context Internal Server Error: /training_details/4 Traceback (most recent call last): File "C:\Users\User\Desktop\webprojects\calendar\venv\lib\site-packages\django\db\models\fields_init_.py", line 1823, in get_prep_value return int(value) TypeError: int() argument must be a string, a bytes-like object or a number, not 'DeferredAttribute' Traceback The above … -
Django Data Aggregation
How would I go about aggregating data from a model and import into a template? I have a model called "RequestAStudent". I want to show a count for absences, tardies, and lateness for each student. See attached screenshot for what I want it to look like in my template. Thanks![Template View][1] class RequestAStudent(models.Model): PERIODS=( ('Academic Network 1', 'Academic Networking 1'), ('Academic Network 2', 'Academic Networking 2'), ) MARK=( ('None', 'None'), ('Present', 'Present'), ('Tardy', 'Tardy'), ('X', 'X'), ('Absent', 'Absent'), ) student = models.ForeignKey(Student, on_delete= models.CASCADE) date_created = models.DateField(auto_now_add=True, null=True) Requesting_Teacher = models.ForeignKey(AdvisoryTeacher, on_delete= models.CASCADE) #AcademicNetwork2 = models.ForeignKey(AdvisoryTeacher, on_delete= models.CASCADE) Period = models.CharField(max_length=18, null=True, choices = PERIODS) teacher = models.ForeignKey(Teacher, verbose_name='Advisory Teacher',default="None", on_delete= models.CASCADE) date_requested = models.CharField(max_length=200, verbose_name='Date for Student Request (Use format MMDDYY)', default="2021-09-16",null=True) attendance = models.CharField(verbose_name='UPDATE ATTENDANCE HERE!',max_length=18, default="None",choices = MARK) #null=True, blank=True, owner = models.IntegerField("Request Owner", blank=False, default=1) def __str__(self): return "" + str(self.student) + ", Teacher: " + str(self.owner) class Meta: unique_together = ["student", "Period", "date_requested"] @user_passes_test(lambda u: u.groups.filter(name='administrators').exists()) def AttendanceByStudent(request): requests = RequestAStudent.objects.all() context={ 'requests':requests, } return render(request, 'attendancebystudent.html',context) I want to output into my template like this: Student Attendance Date Absent Tardy Late +5 -
Custom User Model: No such table exits while creatingsuperuser
I am trying to build a custom user model which I had did successfully in a previous project. But for some reason this is time I am met with this weird error, which I have gone through stackoverflow and find a fix for this. I try to do makemigrations and it shows no migrations to make and I simply do migrate and assume new table is created but then I try to create a superuser and this shows up. So did I miss a step or something? Things that might be informative, I had a previous in built model migrated and trying to create new one by deleting the database file of old one, I have deleted the previous migrations of all other Apps, I have added app and auth_user_model in the settings. Settings.py # Custom Model AUTH_USER_MODEL = "user.CustomUser" # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # Custom "home", "user", ] users/models.py from django.db import models from django.utils.translation import gettext_lazy as _ from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager class CustomUserManager(BaseUserManager): def create_superuser(self, email, password, **other_fields): other_fields.setdefault('is_staff', True) other_fields.setdefault('is_superuser', True) other_fields.setdefault('is_active', True) if other_fields.get('is_staff') is not True: raise ValueError( 'Superuser must be assigned to is_staff=True.') … -
Python, Django Issue while running command : python3 manage.py runserver
I am new to Python and Django. This is my first program in Django and trying to use command (python3 manage.py runserver) but getting below issue. Please help -- enter image description here enter image description here -
django signal post_save is not working while debug is false
I've implemented django signals for a model. It's only working fine while debug is True. Any idea how to fix this issue, so that post_save signal works when debug is False in production. Here is my code in models.py @receiver(post_save, sender=PackageSubscription) def update_balance(sender, instance, **kwargs): current_balance = Balance.objects.get(user=instance.user) last_balance = current_balance.last_balance get_package = Package.objects.filter(title=instance.package) for i in get_package: get_coin = i.coin update_current_balance = last_balance + get_coin if instance.payment_status is True: Balance.objects.filter(user=instance.user).update(last_balance=update_current_balance, updated_at=timezone.now()) Your answer will be highly appreciated. Happy coding :) -
Django/Docker: migration not detected and not applied
Stack: Django/Docker/Docker-compose/Postgresql (not in container) I have made modifications, including models updates, saved and push to my remote Gitlab repository. Then, I pulled modification from my Gitlab repo on the preprod server and I can see that I have the modified version on the server. But when I stop and restart the container, it does not detect any changes and does not apply the migrations. I also checked, the entrypoint.preprod.sh file contains the makemigrations and migrate orders. I have tried by rebuilding docker-compose build then run but it doesn't work anymore. I tried by connecting directly to my container (docker exce -it web sh) but makemigrations are not detetced and migrations are therefore not applied. I should miss something but why? docker-compose-preprod.yml version: '3.7' services: web: restart: always container_name: virage_web build: context: ./app dockerfile: Dockerfile.preprod restart: always command: gunicorn core.wsgi:application --bind 0.0.0.0:8000 volumes: - app_volume:/usr/src/app - static_volume:/usr/src/app/static - media_volume:/usr/src/app/media expose: - 8000 env_file: - ./.env.preprod entrypoint: [ "/usr/src/app/entrypoint.preprod.sh" ] depends_on: - redis healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8000/"] interval: 30s timeout: 10s retries: 50 redis: container_name: virage_redis image: "redis:alpine" celery: container_name: virage_celery build: context: ./app dockerfile: Dockerfile.preprod command: celery -A core worker -l info volumes: - app_volume:/usr/src/app env_file: - ./.env.preprod … -
Create list of objects from csv upload
I have a model called leads. I upload them via CSV. The problem is that I would like to name each of the lead lists when uploading so I can filter and view specific lists. Models.py class Lead(models.Model): first_name = models.CharField(max_length=20) last_name = models.CharField(max_length=20) address = models.CharField(default=0, max_length=50) city = models.CharField(max_length=30, default="") state = models.CharField(max_length=20, default="") zipcode = models.IntegerField(default=0) phone = models.CharField(max_length=10, null=True, default="", blank=True) email = models.EmailField(default="") def __str__(self): return f"{self.first_name} {self.last_name}" View.py def csv_upload(request): template = "leads/lead_csv_upload.html" data = Lead.objects.all() prompt = { 'order': 'Order of the CSV should be name, email, address, phone, profile', 'profiles': data } if request.method == "GET": return render(request, template, prompt) csv_file = request.FILES['file'] if not csv_file.name.endswith('.csv'): messages.error(request, 'THIS IS NOT A CSV FILE') data_set = csv_file.read().decode('UTF-8') io_string = io.StringIO(data_set) next(io_string) for column in csv.reader(io_string, delimiter=',', quotechar="|"): _, created = Lead.objects.update_or_create( first_name=column[0], last_name=column[1], address=column[2], city=column[3], state=column[4], zipcode=column[5], phone=column[6], email=column[7], ) context = {} return render(request, template, context) -
What is the best way to deal with Django form validation involving FieldFile attachments?
I currently have a Django app and am using the built in form validation to generate form errors upon user submission. Everything works fine. My issue is that I am allowing users to attach files to the page...and if the form validation fails, the files disappear and the user has to reattach them and will most likely forget because they already attached them once. Not an optimal user experience. I've seen where I can leverage Javascript, but then doesn't that kinda defeat the purpose of the Django forms? Thanks in advance for any thoughts. -
Problem with Django does not detect change in python code every time need to restart the server
I have a problem my django server does not show the updated python code when refreshing the page everytime i need to restart the server i searched the internet and all what i found is when i run the server ./manage.py runserver it automatically detect any change in the code but i use it and it does not detect the change any help def index(request): name = "David" return render(request, "index.html", {"name": name}) HTML file <!DOCTYPE html> <html> <head> <h1> How are you doing today {{name}} </h1> </head> <body> </body> </html> every time i refresh it shows David D the previous value of name not the new one i have to restart the server to show the new value -
Registered users do not appear in the admin panel
I am working on a project in Django where I am building a form so that users can register on the page; at the time of making a test registration of a user, I can not see the information of the same in the administration panel urls.py from django.urls import path from . import views urlpatterns = [ path('register/', views.registerPage, name="register"), ] views.py from django.shortcuts import render, redirect from django.contrib.auth.forms import UserCreationForm def registerPage(request): form_value = UserCreationForm() if request.method == 'POST': form_value = UserCreationForm(request.POST) if form_value.is_valid(): form_value.save() context = {'form_key':form_value} return render(request, 'accounts/register.html', context) register.html <h3>Register</h3> <form action="" method="POST"> {% csrf_token %} {{form_key.as_p}} <input type="submit" name="Create User"> </form> I don't know if I should register the view in "admin.py" to be able to see the users who do the registration ... Some help would do me good -
Can staticfiles in Django be served in production if they're located in non-english called directories?
In Django I have images stored in directories such as as /items/static/items/images/something_russian/image.jpg. And it appears Django doesn't want to serve these files in production when their path is non-english. Works perfect in development, and also works if no non-english path is used. What's the best way to fix the problem? Using Django==3.2.4 and whitenoise==5.2.0. Images on the same server as the project. -
I can not see running docker compose containers on ubuntu 20.04
I have web app in 3 containers running on linux server (ubuntu 20.04), Django webapp, nginx webserver and postgres db. When i run 'docker-compose ps' it does not show any container. I am sure that it is the right folder as there is no other docker-compose.yml on this server. It seems almost like app is not running there except that it is accessible via browser and working well. I tried all kinds of commands for showing containers or images using docker and docker compose with no result I tried 'docker service restart' - app went offline for a moment and then back online (I have 'restart: allways' set in compose file) also I restarted whole server with same result. Even script which is making db dumb does not see containers and started do fail When I try to bring up the project running 'docker-compose up' webapp and db containers starts but webserver not because port is already taken Have anyone experienced this? I work with docker compose for a while but it never happend to me before, I dont know what to do, I need to update code of this application and I dont want to loose data in DB …