Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
I am trying to use my existing login page for my django application for wagtail, butI am still seeing the default wagtail login form
I am using wagtail 2.9.1. I have added the following lines of code to my base.py configuration file. WAGTAIL_FRONTEND_LOGIN_TEMPLATE = 'my_app_name/templates/account/login.html' and WAGTAIL_FRONTEND_LOGIN_URL = '/accounts/login/' I have also tried WAGTAIL_FRONTEND_LOGIN_URL = LOGIN_URL I am still seeing the default wagtail login form when I try and login to the cms. This is the documentation I am triyng to follow. Am I missing something? -
Scheduling management command with django_crontab package
Eventually the management command will be used to scrape data and populate a db once a week, but for now I am just trying to print on a scheduled basis. Here is the management command (located at player/management/commands/update_data.py): from django.core.management.base import BaseCommand class Command(BaseCommand): def handle(self, *args, **options): print('This is working') Now to the scheduling part... I ran python3 -m pip install django-crontab Added the following to settings.py: INSTALLED_APPS = [ 'django_crontab', ] CRONJOBS = [ ('*/1 * * * * ', 'django.core.management.update_data'),# I believe what I am saying here is run update_data every minute? ] and finally python manage.py crontab add which returned adding cronjob: (1be0bb6367b686fd03a204d920284ed3) -> ('*/1 * * * * ', 'django.core.management.update_data') I then waited a minute and "this is working" was never printed to the console. If anyone is familiar with this django-crontab (https://pypi.org/project/django-crontab/) package or knows another way to schedule management commands, help would be much appreciated. Thanks in advance. -
How to filter products by either brand or category in django
I'm working on a Django blog, and I want to be able to filter products either by categories or brands. Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/nestle/ Raised by: shop.views.product_list No Category matches the given query. The code works perfectly for a single instance but fails when I try to use both instances simultaneously. i.e. If I should comment either the brand or category, the code works well but both can't work together. **Request URL: http://127.0.0.1:8000/nestle/ ** -- nestle is a brand but the error raised is No Category matches the given query.. I wanted it to filter by brand and not by category This is my views.py file def product_list(request, category_slug=None, brand_slug=None): category = None categories = Category.objects.all() products = Product.objects.filter(available=True) brand = None brands = Brand.objects.all() products = Product.objects.filter(available=True) if category_slug: category = get_object_or_404(Category, slug=category_slug) products = products.filter(category=category) elif brand_slug: brand = get_object_or_404(Brand, slug=brand_slug) products = products.filter(brand=brand) return render(request, 'shop/product/list.html', {'category': category, 'categories': categories, 'brand': brand, 'brands': brands, 'products': products}) This is my model.py from django.urls import reverse class Category(models.Model): name = models.CharField(max_length=200,db_index=True) slug = models.SlugField(max_length=200,unique=True) class Meta: ordering = ('name',) verbose_name = 'category' verbose_name_plural = 'categories' def __str__(self): return self.name def get_absolute_url(self): return reverse('shop:product_list_by_category',args=[self.slug]) … -
how to send images from reactJS to Django rest framework
I have problem sending an image from reactJS to DRF3, my code is below, please help me. DRF3 code serializers.py from rest_framework.serializers import Serializer, FileField class UploadSerializer(Serializer): """serialzer to upload files""" file_uploaded = FileField() class Meta: fields = ["file_uploaded"] views.py from django.shortcuts import render from rest_framework import status from rest_framework.viewsets import ViewSet from rest_framework.response import Response from django.core.files.storage import FileSystemStorage from .serializers import UploadSerializer class UploadViewSet(ViewSet): """view to manage images""" serializer_class = UploadSerializer def list(self, request): """list images uploaded""" return Response("get api list") def create(self, request): try: file_uploaded = request.FILES.get("file_uploaded") f_s = FileSystemStorage() filename = f_s.save(file_uploaded.name, file_uploaded) image_url = f_s.url(filename) return Response({"data": f"{image_url}"}, status=status.HTTP_201_CREATED) except Exception as err: return Response( {"data": False, "msg": f"{err}"}, status=status.HTTP_404_NOT_FOUND ) urls.py from django.urls import path from .views import UploadViewSet app_name = "uploaded-images" urlpatterns = [ path( "images", UploadViewSet.as_view({"post": "create"}), name="create-upload-images" ) ] here everythings working when I use postman, working well, but the problem is when send image through reactJS, my code is bellow file send image with typescript sendImage.tsx import API from '../api-img' // my IP SERVER, working export class UploadFiles { private resp: any uploadImg = async(file_upload:any) => { this.resp = await API.post(`upload/images`, file_upload, { headers: {'Content-Type': 'multipart/form-data; boundary=--- WebKitFormBoundary7MA4YWxkTrZu0gW'} }) return … -
Serialize Json file with nested clases in Django Rest API
I hope you are doing well. I need your help, please! I’m very new in rest API and I need your help! I’m developing a web page with Django and for fill up the database I have a Json file. I’m using REST API to serialize the information. The Json file has the following structure: { "image": "T_11_C_3.jpeg", "bounding_boxes": [ { "position_3d": [ 0, 0, 0 ], "position_top_left": [ 1446, 1914 ], "size": [ 462, 463 ], "class": 68541403, "class_list": [ 68541403, 68533773, 68533703, 68543685, 68539982, 68537973, 68541872, 68537686, 68533486, 68535516 ] }, { "position_3d": [ 0, 0, 0 ], "position_top_left": [ 1259, 1918 ], "size": [ 458, 458 ], "class": 68540472, "class_list": [ 68540472, 68539961, 68540045, 68540633, 68543755, 68543713, 68540073, 68540024, 68540031, 68540360 ] } ] } Where the file contains the data of "MainPlanogram.class", "bounding_boxes" is the "Planogram.class" and the integers of "class" and "class_list" are ids of "Product.class" as you can see in the following Model that represents JsonFile: class Product(models.Model): image_product = models.ImageField( upload_to=upload_location_image_product, null=False, blank=False) class Planogram (models.Model): position_3d = ArrayField(models.IntegerField(blank=True), size=3,) position_top_left = ArrayField(models.IntegerField(blank=True), size=2,) size = ArrayField(models.IntegerField(blank=True), size=2,) product_class = models.ForeignKey( Product, null=True, on_delete=models.SET_NULL, related_name='product_class') product_list_class = ArrayField(models.IntegerField(blank=True), size=10,) class MainPlanogram (models.Model): image … -
basic django google maps field
I'm looking for a way to integrate a simple google maps field in one my models to be able to add locations and their long/lat data into my database from the admin area. I don't need anything too extensive like GeoDjango, that is an overkill for my needs. I already have my google maps api and a map rendered on my website ready to add points. Hoping that someone knows of a simple solution. Thanks! -
Database error while trying to register/login from django app hosted in GCP
I have deployed django app in Google cloud Platform,I am using mongodb atlas to store users data .I can successfully register and login by running it on localhost.But when i try to register/login directly from the app url ,it's giving database error.In settings.py file ,i have given connection string of mongodb database.How would i be able to access data stored in mongodb through app deployed in gcp ? Would you please help me out regarding that.Thanks -
Django: Using decorators on methods in a subclass to handle requests from urls.py
I used to have some massive files with decorated funtions called from urls.py. This wasn't sustainable so they were split into controllers, with logic somewhere else (not relevant for this question). The controllers are subclassed from a main Controller class to share frequent imports and other commonalities. I can't get the decorator functions to work now, the closest I've gotten are AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `api.controllers.FooController.FooController`. and AttributeError: 'functools.partial' object has no attribute '__name__' Neither using a get() with as_view() nor a custom method work, as seen below. I've tried @method_decorator(login_required) etc. with no success. My files (simplified) are below. Do not mind the method contents, this is just for demonstration. api/urls.py: from django.urls import path from api.controllers.FooController import FooController urlpatterns = [ path("get_foo", FooController,as_view(), name="get_foo") path("get_bar", FooController.foo_bar, name="get_bar") ] api/controllers/Controller.py: from django.views import View from rest_framework.authentication import SessionAuthentication, TokenAuthentication from rest_framework.decorators import api_view, authentication_classes, permission_classes from django.http import HttpResponse, JsonResponse, StreamingHttpResponse from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator class Controller(View): def __init__(self): # Set some variables # define some methods api/controllers/FooController.py: from api.controllers.Controller import * class FooController(Controller): method_decorator(api_view(["GET"])) method_decorator(login_required) method_decorator(authentication_classes((SessionAuthentication, TokenAuthentication,))) method_decorator(permission_classes((IsAuthenticated,))) def get(self, request): if request.user.is_authenticated: return HttpResponse(status=200) else: return … -
Apply permission to an endpoint in a RBAC API Django Rest Famework
I have an API with RBAC. Today i have 3 roles: b2b, b2c and admin. Theoretically, my admin user have the power to grant and deny the access for the b2b and b2c to specifics endpoints. So, for the b2b user to access the endpoint X, it must have the permission X, that can be granted or removed by the admin user. How can i create, DYNAMICALLY, a permission for each of my endpoints ? Assuming that i have the relation of all the permissions needed for each endpoint, how can i validate the access without hitting the database twice, at first to get the permissions needed to access the endpoint and the second time to get the users permissions ? As far as i know, the django permissions is only object level. There's any lib or extension that already implements the url(endpoint) level permission? -
Django - multiply two queryset
I have two models: Product: Name = charfield Price = floatfield Sales: Product = foreignkey(product) Quantité = floatfield I defined two queryset: Product = Product.objects.all() Sales = Sales.objects.all() How can I have a queryset of the total amount sold by product ? Something like product x sales -
In django, when posting a new object via a form, how do you manually enter in its foreign keys?
In my case, I have two models: a parent and a child. On my post page, I have made it so that both the parent and multiple child class are posted into my database. The child classes have the parent as a foreign key. How do I reference the foreign key in my child model? -
Why does Django create migrations in the parent model's app when they are inherited?
I have the following setup, an external dependency dep is installed and being used in my Django project and I'm extending one of it's models in my own apps, like the following: from dep.models import ParentModel class MyModel(ParentModel): # fields Both mine and the dependency models are not abstract and by running makemigrations a new migration is created into the dep app, following it's current migration tree. The problem is that I expect to have a new migration in my own app as it doesn't make sense to mess with the dependency's migration structure as it's gonna create all sorts of problems whenever it's updated. Is there a way to go around this? I've tried moving the migrations manually to my app but when I run makemigrations again, it gets deleted and created again the same way as I mentioned above. -
PhoneNumberField - not saving the telephone number's extension
In a django application, I use PhoneNumberFields modelfields and formfields. When entering a telephone number in a form (e.g. +15146661234x99), the extension (i.e. 99) is not saved in the model. What am I doing wrong? Here is an extract from my code: models.py from phonenumber_field.modelfields import PhoneNumberField class Profile(models.Model): user = models.OneToOneField(User, null=True, on_delete=models.CASCADE) phone = PhoneNumberField(_('Telephone'), max_length=30, null=True, blank=True, unique=False) views.py def userUpdate(request, pk): template = 'plans/form_user.html' context = {} user = User.objects.get(pk=pk) profile = user.profile form = ProfileForm(request=request, instance=profile) if request.method == 'POST': form = ProfileForm(request.POST, request=request, instance=profile) if form.is_valid(): form.save() messages.success(request, _("Information of administrator {} has been updated").format(user)) context['message_tag'] = 'success' return redirect('user_list') context['user'] = user context['form'] = form return render(request, template, context) forms.py from phonenumber_field.formfields import PhoneNumberField class ProfileForm(ModelForm): class Meta: model = Profile fields = ['phone',] def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super().__init__(*args, **kwargs) self.fields['phone'] = PhoneNumberField( label=_('Telephone number'), help_text=_('Enter a valid phone number, e.g. +14185551234x22') ) form_user.html {% extends 'plans/base.html' %} {% load i18n %} {% load crispy_forms_tags %} {% block content %} <form method="POST" action=""> {% csrf_token %} <div class="mb-2"> <button class="btn btn-warning" type="submit"><i class='fas fa-check fa-lg text-white'></i></button> <a class="btn btn-sm btn-info p-2" style="font-weight: bold;" href="{% url 'user_list' %}">{% trans 'Back' %}</a> … -
Used manage.py dbshell -> DROP TABLE to delete a database table, now I cannot recreate it
I am trying to use a custom Django management command to populate a db with scraped data. I messed this up on the first go so I deleted the table using python manage.py dbshell and then DROP TABLE player_player (Player is the name of the model). The table is now gone but when I re-run python manage.py makemigrations and python manage.py migrate it does not seem to recreate it. I tried deleting the migrations in the player application and ran python manage.py makemigrations again and it returned Migrations for 'player': player/migrations/0001_initial.py - Create model Player but when I run python manage.py migrate it says Running migrations: No migrations to apply and does not recreate the Player db table. Can anyone help with this? I was getting an error I couldn't solve so I figured I'd delete the table and try again since there was no data I cared about in there, but now think that was a poor decision. -
Used manage.py dbshell -> DROP TABLE to delete a database table, now I cannot recreate it
I am trying to use a custom Django management command to populate a db with scraped data. I messed this up on the first go so I deleted the table using python manage.py dbshell and then DROP TABLE player_player (Player is the name of the model). The table is now gone but when I re-run python manage.py makemigrations and python manage.py migrate it does not seem to recreate it. I tried deleting the migrations in the player application and ran python manage.py makemigrations again and it returned Migrations for 'player': player/migrations/0001_initial.py - Create model Player but when I run python manage.py migrate it says Running migrations: No migrations to apply Can anyone help with this? I was getting an error I couldn't solve so I figured I'd delete the table and try again since there was no data I cared about in there but now think that was a poor decision. -
How to use Ajax to send and fetch data from the database in Django, but without JQuery?
I am currently working on a Django website, and I want to send the information that the user enters in a form to a Django view where the data will be stored in the database. Also, I want to fetch data from the database and display it on my website. I want to achieve all this by using Ajax, but without JQuery. I don't want a page refresh and to achieve this I have to use Ajax. I did surf the internet for the same but all the tutorials that I read implemented this behaviour using JQuery and Ajax. And, I want to do this without JQuery. So, how can I do this? Also if there is some other better way to implement the same, please suggest. -
AttributeError: type object 'User' has no attribute 'USERNAME_FIELD'
This is my code. I understand that the User class needs USERNAME_FIELD, but I have that, so I am not sure exactly what the issue is. Any help is greatly appreciated. enter image description here -
Calling method of object in Django template
Having trouble calling methods of non-Django classes in django template. I've tried both calling it as an attribute and creating a custom filter of which neither I can get to return anything. Apologies if this is obvious but I'm not well versed in Django and I can't seem to find any reference of non Django model classes in their documentation I have a class class MarketDataSet(): def __init__(self, market_data): self.market_data = market_data def filter_data(self, side): filtered = list() for dp in self.market_data: if dp.side == side: filtered.append(dp) return filtered def bids(self): return self.filter_data(side='bid') def asks(self): return self.filter_data(side='bid') def trades(self): return self.filter_data(side='trade') def high_bid(self): amounts = list() if not self.bids(): return '' else: for bid in self.bids(): amounts.append(bid.amount) return max(amounts) def low_ask(self): amounts = list() if not self.asks(): return '' else: for ask in self.asks(): amounts.append(ask.amount) return max(amounts) def average_trade(self): amounts = list() if not self.trades(): return '' else: for trade in self.trades(): amounts.append(trade.amount) return statistics.mean(amounts) and views.py (relevant portion): if form.is_valid(): style_id = form.get_style().upper() stats = dict() st_data = MarketDataSet(market_data=get_full_market_data(style_id=style_id, size=None)) stats.update({'st': st_data}) return render(request=request, template_name='main/pricer_response.html', context={'form': form, 'stats': stats, }) and pricer_response.html template (relevant portion) Notice both approaches: {% for platform in stats.keys %} {% block content %} {% … -
django-rest-framework-social-oauth2: Where to get <google_token>?
I am trying to use Google OAuth2 for my Django REST API. I am using the django-rest-framework-social-oauth2 library and I am having trouble implementing their Google solution. The problem I am having is simple: I don't know where to get what they call <google_token> in the request they make to get the "access token"... curl -X POST -d "grant_type=convert_token&client_id=<django-oauth-generated-client_id>&client_secret=<django-oauth-generated-client_secret>&backend=google-oauth2&token=<google_token>" http://localhost:8000/auth/convert-token I have registered my app on Google API Console and I have all the OAuth2 credentials including Client ID and Client Secret. Where can I get <google_token>? Can anybody point me to a working tutorial regarding this? -
Django: Rendering AWS S3 static files through css background-image
I was having some difficulties rendering my static data through the css background-image tag. For example, take the following piece of code: <section class="banner-area" style="background-image: url('../../static/main/img/search4.jpg')>; When I inspect the element on my browser, I can see that it is not being linked to AWS S3 bucket, and the url remains exactly the same as per the following: url('../../static/main/img/search4.jpg') However if I render an image using the source tag, I get the behaviour I want, for example: <img src="{% static 'main/img/search4.jpg' %}/> Here, when inspecting the element, I can see in the browser that it is being linked to my S3 Bucket as per the following: src="https://mybucket-bucket.s3.amazonaws.com/main/img/search4.jpg?ETC...." In my settings.py STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATICFILES_DIRS = ( os.path.join(BASE_DIR, "main/static"), ) MEDIA_ROOT = os.path.join(BASE_DIR, 'static/img') MEDIA_URL = "/media/" DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' STATIC_URL='https://mybucket-bucket.s3.us-east-2.amazonaws.com/' Can you please advise ? Kind regards, Marvin -
Why is uploading a video to azure blob storage this slow? [DJANGO]
I have a problem when uploading a video to the Azure blob storage with Django. I am not sure if it has something to do with the Django framework being slow or my code. btw, it is a project from me and my friend and he did the most work. So it's possible that I don't know everything. :) When uploading a video of like 1GB it will take around 2/3 minutes. What I have looked into but not implemented yet: AZCopy multithreading video model: def create_file_path_video(instance, name): return os.path.join('sources', str(instance.pk), name) class Video(models.Model): video = models.FileField(upload_to=create_file_path_video, validators=[validate_video_file_extension], null=True) name = models.CharField(max_length=255, null=True) storage_name = models.CharField(max_length=255, null=True) size = models.BigIntegerField(null=True) fps = models.FloatField(null=True) frames_count = models.FloatField(null=True) duration = models.FloatField(null=True) type = models.CharField(max_length=10, null=True) uploaded_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=False) uploaded_at = models.DateTimeField(default=datetime.now, null=True, blank=True) deleted_at = models.DateTimeField(null=True, blank=True) def __duration(self): return int(self.frames_count/self.fps) Form: class UploadVideoForm(forms.ModelForm): class Meta: model = Video fields = ['video'] widgets = { 'name': forms.HiddenInput(), 'uploaded_by': forms.HiddenInput(), 'uploaded_at': forms.HiddenInput(), 'type': forms.HiddenInput(), 'size': forms.HiddenInput() } View: def video_create_view(request): context = {} form = UploadVideoForm(request.POST or None, request.FILES or None) if request.user.is_superuser or request.user.groups.filter(name="Editor").exists(): if request.method == 'POST': form = UploadVideoForm(request.POST, request.FILES) video_obj = Video.objects.create() if form.is_valid(): name = … -
Python Django ModuleNotFoundError for modules containing underscores only on CI pipeline
I have been hitting the ModuleNotFoundError error when I add modules to my project in the requirements.txt file. This seems to only occur with module imports that have an underscore. Namely these two: sentry_sdk exponent_server_sdk When I create virtual environment locally and run server it works without error. When the same process is done as part of my CI pipeline I get: ModuleNotFoundError: No module named 'sentry_sdk' I have followed the sentry guide for django integration https://docs.sentry.io/platforms/python/guides/django/ I'm using buddy.works as my pipeline. I Googled around and it doesn't seem to be a common issue. I'm also confused that other modules I have worked with have worked straight away such as stripe. I can only think it has something to do with the underscores? Or possibly there is an issue with a path somewhere on the CI build. Any clues is appreciated, thanks. -
Add new object to list without refreshing wit Django & Ajax
I'm searching for someone who helps me in a Django project with JS, Ajax and jquery. I'm trying to create something like adding objects on the django-admin page. I used https://www.pluralsight.com/guides/work-with-ajax-django, and almost everything is working fine but... On my form, I have multiple choice field with authors, when I pressed the button to add a new author, the object is properly saving in DB, but on the form, I can't see a new object. When I reload the page, the new object is on this multiple choice field. I thought I should refresh the field to see a new object on the list, but I don't know it's the proper way to meet this goal. -
How to pass a request.get into action attribute of form tag as i am new to Django
Template <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> </head> <body> <form action = "{% url "sparta:storeid" store_id=request.GET.get('your_name') %}" method = "get"> <label for="your_name">Your name: </label> <input type="text" name="your_name"> <input type="submit" value="OK"> </form> </body> </html> Views.py from django.shortcuts import render from django.http import HttpResponse def detail(request,store_id='1'): print(store_id) return HttpResponse("checkout {}".format(store_id)) def forms(request): dict1={'con':'sdsd'} return render(request, "forms.html",context=dict1) urls.py (from application) from django.conf.urls import url from second_app import views app_name='sparta' urlpatterns=[ url(r'^stores/$',views.forms), url(r'^stores/(?P<store_id>\d+)/$',views.detail,name="storeid"), ] urls.py (from main url) from django.conf.urls import url,include from django.contrib import admin from django.urls import path from second_app import views urlpatterns = [ url(r'^stores/',include("second_app.urls")), path('admin/', admin.site.urls), ] I am able to get results while using request.GET.get('your_name') in views.py and using render. I am experimenting for this case from directly passing the request variable through url tag . -
Problem with generating PDF using xhtml2pdf in Django
I've tried to generate a PDF using xhtml2pdf in Django, based on the data that I'll fetch in request.POST. I used to send data using HTML form and an submit button to submit data to the view. Everything was and I got the generated PDF attached to the browser. But here is the problem, I've tried to use AJAX, seems everything is OK and I got rendered PDF. but the PDF does not attach. I think something behind the scenes happens when I send using normal HTML form that won't happen by using Ajax. def create_pdf(request): data = json.loads(request.POST["courses"]) context = { "courses": data } template = "teachers/course_template.html" pdf = render_to_pdf(template, context) if pdf: response = HttpResponse(pdf, content_type="application/pdf") filename = "plan.pdf" content = "attachment; filename=%s" % filename download = request.GET.get("download") if download: content = "attachment; filename=%s" % filename response["Content-Disposition"] = content return response return HttpResponse("Not found.") def render_to_pdf(template_src, context_dict={}): template = get_template(template_src) html = template.render(context_dict) result = BytesIO() pdf = pisa.pisaDocument( BytesIO(html.encode("UTF-8")), result, encoding="UTF-8") return HttpResponse(result.getvalue(), content_type="application/pdf") I send data this way: $(".btnCreatePlan").click(function (e) { e.preventDefault() // Send workouts to the view var CSRFToken = $("input[name=csrfmiddlewaretoken]").val(); var parameters = { "workouts": JSON.stringify(workouts), "csrfmiddlewaretoken": CSRFToken }; $.ajax({ url: "http://localhost:8000/teachers/requests/create/pdf/", method: "post", …