Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
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 … -
django_migrations table and several apps / databases
I have 3 apps and 3 databases setup in my Django project. Each app is related to its own DB so I created a DB router and all seems to work fine but I still see the migrations of every app being recorded in the django_migrations table of each DB. Is that the normal behavior? -
Why does AWS give me an error with regions?
This has happened to me everytime i connect my django to aws S3 buckets, normally it goes away for some reason which I don't know and would like to know how to prevent it from happening again. my settings: AWS_S3_REGION_NAME = 'eu-west-3' AWS_S3_SIGNATURE_VERSION = 's3v4' AWS_S3_FILE_OVERWRITE = False AWS_DEFAULT_ACL = None (I am not including the access key id, secret access key and bucket name, they are not necessary) and my Bucket regiion is: EU (Paris) eu-west-3 my full error message: <Code>AuthorizationQueryParametersError</Code> <Message>Error parsing the X-Amz-Credential parameter; the region 'us-east-1' is wrong; expecting 'eu-west-3'</Message> <Region>eu-west-3</Region> I have seen online explanations but none seemed to solve it. As I said this has happened to me multiple times and always goes away for no reason! I would like to know what causes it rather than leaving it up to chance. Does anyone have any idea why? -
How to manage sessions with Firebase Authentication, React SPA and Django Rest Framework Backend?
I think the most important details are in the title. I'm well aware that this combination is mostly not recommended. Therefore I would be very happy if someone might come up with an alternative way of solving the detailed problem that follows: The SPA and the Backend-API are running on different hosts provided by Google Cloud Services (Flexible Google App Engine). We have a user account that is supposed to run on several devices. One device will be used as day-to-day operations in a store by the manager. When the store owner (who might be different from the manager) wants to change Store-Properties he should be able to login on a different device and request a TAN for an admin session. With Firebase Custom Claims we can make the admin session available to the account, but this would mean that the store manager can also access the properties in the current session. This shouldn't be possible. That's why we need a session-based management of user access. The only option I see this far is having Django assigning sessions, which means we have to mess with CORS and reduce Django's security measures. The resources available to me generally and on Google … -
django datefield auto_now = true dont work
I have this table in my model, i just want that if the data is updated the modifyDate field will automatict updated. note: i am not using the adminsite to update data class FmCustomerEmployeeSupplier(models.Model): dateSubmitted = models.DateField(auto_now_add=True, null=True, blank=True) lastname = models.CharField(max_length=500, blank=True, null=True) firstname = models.CharField(max_length=500, blank=True, null=True) middleInitial = models.CharField(max_length=500, blank=True, null=True) bodyTemperature = models.FloatField(blank=True, null=True) modifyDate = models.DateField(auto_now=True, blank=True, null=True) modifyBy = models.CharField(max_length=500, blank=True) @property def is_past_due(self, *args, **kwargs): return date.today() > self.modifyDate class Meta: verbose_name_plural = "50. Customer's List of Employees, Suppliers and visitors" -
Best way of saving edit history in postgresql with Django
A lot of times I've had the need to know "who updated what" in my database tables. I've always done this by creating similar tables with extra fields, for instance "updated_by" and "time_updated" which store the user who edited the record and when they did it respectively. Here's a simple example; CREATE TABLE student ( id SERIAL PRIMARY KEY, first_name varchar(100) PRIMARY KEY, last_name varchar(100), ); CREATE TABLE student_edit_history ( id SERIAL PRIMARY KEY, student_id integer, first_name varchar(100) PRIMARY KEY, last_name varchar(100), updated_by varchar(100), time_updated timestamptz, ); Is there a better way of achieving the same in PostgreSQL without creating new tables? Does Django provide a way of saving edit history? -
one to Many relationships in django
I'm trying to display multiple fields on a single line in django admin. it must be like so: Listaflor: Flora2Estado object (1) Familia object (1) Flora2Estado object (2) Flora2Estado object (1) Flora2Estado object (1) Familia object (1) My db looks like this: Estados: Estado_id Estado_nome Flora2Estado: estado_id especie_id models.py: especie = models.OneToOneField(Flora2Estado, models.DO_NOTHING, primary_key=True)``` -
Show multiple fields within a row in Django Admin
there. I've been struggling with a situation in Django/Mysql. There is this column in a table that has a primary key and foreign key at the same time. This column has a one to many relation with an intermediary table. It's a list of states linked to plant species. Some species can be found in more than one state. Species (table 1) Columns: Species_id | specie_name Species_to_states (table 2) Columns: Species_id | State_id States (table 3) Columns: States_id, State_name The point is that it's being shown only one object in django admin. So i would like to show multiple fields within a row. Can someone help me with that issue? Thank you so much -
list() missing 1 required positional argument: 'slug'
In my Django projects to effectively teake a random objects form queryset I have always used: random_cars = random.sample(list(Cars.objects.all()), 3) In the current project, this is raised by an exception: list() missing 1 required positional argument: 'slug' I tried to add the slug = None attribute but to no work random_cars = random.sample(list(Cars.objects.all(), slug=None), 3) it's return uerySet' object has no attribute 'META' How can I efficiently get a random query slice without error. Why can I get this error? -
Trim off N bytes from audio file using SoX / FFmpeg etc, on Windows?
My team accidentally on purpose clicked NO when Audacity asked to save the recording. So I left with bunch of *.au files, after using recovery program. Some of them did have header and still open-able with audacity itself (example : this one), and some other are just complete nonsense, sometimes having the header filled with text from random javascript or HTML code (like this one). Probably hard disk half overwritten with browser cache? I don't know. And at this point, I almost don't care. The audacity is on default settings, with sample rate 44100Hz. I can open a-113.au using audacity, from standard open files. I also was able to achieve open files using "Open RAW files" from Audacity, using this settings : so I assume header takes 12384 bytes. Now, how do I trim 12384 bytes from the file when opened as RAW, with either SoX or ffmpeg? because if I open it as RAW with 0 offset (default settings), it will add the header as a noise. Current ffmpeg command I used : ffmpeg.exe -f f32le -ar 44.1k -ac 1 -i source destination Current sox command I used : sox -t raw --endian little --rate 44100 -b 1 -b … -
django-oauth-toolkit request object don`t have custom attribute added by middleware
I have created a middleware and added my_name attribute in request and accessing this in custom authentication class but getting attribute error. class MyMainMiddleware(MiddlewareMixin): def process_request(self, request): request.my_name = "my name" added middleware MyMainMiddleware in settings MIDDLEWARE = [ "apps.middleware.MyMainMiddleware", "django.middleware.security.SecurityMiddleware", 'corsheaders.middleware.CorsMiddleware', "django.contrib.sessions.middleware.SessionMiddleware", "oauth2_provider.middleware.OAuth2TokenMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", ] AUTHENTICATION_BACKENDS = [ "apps.accounts.backends.ModelBackend", ] views.py from oauth2_provider.oauth2_validators import OAuth2Validator from django.contrib.auth import authenticate class OAuth2Validator(OAuth2Validator): def validate_user(self, username, password, client, request, *args, **kwargs): """ Check username and password correspond to a valid and active User """ u = authenticate(request, username=username, password=password) if u is not None and u.is_active: request.user = u return True return False class CustomTokenView(TokenView): validator_class = OAuth2Validator @method_decorator(sensitive_post_parameters("password")) def post(self, request, *args, **kwargs): return super(CustomTokenView, self).post(request, *args, **kwargs) curl request for token curl -X POST \ http://localhost:8000/authenticate/token/ \ -F grant_type=password \ -F username=<user> \ -F password=<password> \ -F client_id=<client_id> \ -F client_secret=<client_secret> Below is the traceback File "/usr/local/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/grant_types/resource_owner_password_credentials.py", line 184, in validate_token_request request.password, request.client, request): File "/code/apps/accounts/views.py", line 39, in validate_user u = authenticate(request, username=username, password=password) File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/__init__.py", line 73, in authenticate user = backend.authenticate(request, **credentials) File "/code/apps/accounts/backends.py", line 16, in authenticate if username is None: File "/usr/local/lib/python3.7/site-packages/oauthlib/common.py", line 436, in __getattr__ raise AttributeError(name) AttributeError: my_name … -
django datefield auto_now = true dont work
I have this field in my models , i just want that if the data is update on that table, the django will trigger or the modifyDate will automatic updated as well. note: i am not using the adminsite to update data class FmCustomerEmployeeSupplier(models.Model): dateSubmitted = models.DateField(auto_now_add=True, null=True, blank=True) lastname = models.CharField(max_length=500, blank=True, null=True) firstname = models.CharField(max_length=500, blank=True, null=True) middleInitial = models.CharField(max_length=500, blank=True, null=True) bodyTemperature = models.FloatField(blank=True, null=True) modifyDate = models.DateField(auto_now=True, blank=True, null=True) modifyBy = models.CharField(max_length=500, blank=True) @property def is_past_due(self, *args, **kwargs): return date.today() > self.modifyDate class Meta: verbose_name_plural = "50. Customer's List of Employees, Suppliers and visitors"