Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
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", … -
Django channel consumers of multiple channels send data to websocket clients but all data is sent to lastly connected websocket client
I use django channels 3.0.0 and websocket using angular. But users connect to django websocket and they are in their own rooms respectively. And when I want to send the event to connected users outside of consumers, I used the following code. all_users = list(Member.objects.filter(is_active = True).values_list('id', flat = True)) for user_id in all_users: async_to_sync(channel_layer.group_send)( "chat_{}".format(user_id), { "type": "tweet_send", "message": "tweet created" } ) And in consumers.py, my consumer class's chat_message function async def tweet_send(self, event): content = event['message'] # Send message to WebSocket await self.send(text_data=json.dumps({ "type": "MESSAGE", "data": content })) And this "self.send" function is meant to be sent to all connected users respectively, but when I run the code, the all data are sent to only one user who has connected the last time. I don't know why. If anyone knows the reason, please help me. -
How to send a secured parameter/value to javascript from django
Is it possible to send a value from django to javascript and have it hidden? view.py def index(request): . . return render(request, "index.html",{'secret_message':"PerfectStorm"}) JavaScript (index.html) <script> . . const message= '{{secret_message}}' </script> If you click the development mode in chrom - one can see the value of message. is there any 'as_hidden' for the message parameter? -
list_filter with choices as values
I have a Billing model with a boolean field class Billing(models.Model): ... is_paid = models.BooleanField(verbose_name='Statut', default=False, choices=[(True, 'Payée'), ('False', 'Non payée')]) ... This is Django Admin class BillingAdmin(BaseOnlyModelAdmin): ... list_display = ['month', 'year', 'date', 'is_paid'] list_filter = ['is_paid'] ... is_paid is correcly displayed as Payée / Non payée when listing But for list_filter it's translated to Oui / Non How can I change this behaviour ? I want it to display choices, so it would look like this -
APScheduler vs Sched Module vs Cron
Could anyone please explain the pros/cons when using APScheduler VS Sched Module VS Cron with Django 3? I am building a blog application using Django Rest Framework and MySQL 8. Application is fairly straightforward CRUD. User can post a blog post, update, delete. Each blog post can have any one of these statuses published, draft, delete and archived. Now I need a scheduler that starts at 11:59 PM every day to move post status from published to archived (posts that have the status published and were posted 3 months ago and the number of comments are less than 10). Could you please explain in detail what differentiates each kind of scheduler? Which configuration should scale the best? PS: If you need more details please ask me in the comments. -
Scrapy Spider stops before crawling anything
So I have a django project and a views.py from which I want to call a Scrapy spider if a certain condition is met. The crawler seems to be called just fine but terminates so quickly that the parse function is not called (that is my assumption at least) as shown below: 2020-11-16 18:51:25 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'products', 'NEWSPIDER_MODULE': 'crawler.spiders', 'SPIDER_MODULES': ['crawler.spiders.my_spider'], 'USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, ' 'like Gecko) Chrome/80.0.3987.149 Safari/537.36'} 2020-11-16 18:51:25 [scrapy.extensions.telnet] INFO: Telnet Password: ****** 2020-11-16 18:51:25 [scrapy.middleware] INFO: Enabled extensions: ['scrapy.extensions.corestats.CoreStats', 'scrapy.extensions.telnet.TelnetConsole', 'scrapy.extensions.logstats.LogStats'] ['https://www.tesco.com/groceries/en-GB/products/307358055'] 2020-11-16 18:51:26 [scrapy.middleware] INFO: Enabled downloader middlewares: ['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware', 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware', 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware', 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware', 'scrapy.downloadermiddlewares.retry.RetryMiddleware', 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware', 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware', 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware', 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware', 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware', 'scrapy.downloadermiddlewares.stats.DownloaderStats'] 2020-11-16 18:51:26 [scrapy.middleware] INFO: Enabled spider middlewares: ['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware', 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware', 'scrapy.spidermiddlewares.referer.RefererMiddleware', 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware', 'scrapy.spidermiddlewares.depth.DepthMiddleware'] views.py def get_info(): url = data[product]["url"] setup() runner(url) @wait_for(timeout=10.0) def runner(url): crawler_settings = Settings() configure_logging() crawler_settings.setmodule(my_settings) runner = CrawlerRunner(settings=crawler_settings) d = runner.crawl(MySpider, url=url) my_spider.py import scrapy from scrapy.loader import ItemLoader from itemloaders.processors import TakeFirst from crawler.items import ScraperItem class MySpider(scrapy.Spider): name = "myspider" def __init__(self, *args, **kwargs): link = kwargs.get('url') self.start_urls = [link] super().__init__(**kwargs) def start_requests(self): yield scrapy.Request(url=self.start_urls[0], callback=self.parse) def parse(self, response): do stuff Can anyone guide me towards why this is happening and how I can …