Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Mocking django Form clean_data field
I have a simple django form like this: class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField(widget=forms.Textarea) My view uses it like this: def my_view(request): form = ContactForm(request.POST) if form.is_valid(): data = form.cleaned_data ... I want to test my view and don't care about what the form actually does. This is what my test looks like so far @patch.object(ContactForm, 'is_valid') def test_my_view(mock_is_valid): is_valid.return_value = True ... assert response.status_code == 201 However, this doesn't work because form.cleaned_data is not set until form.is_valid() is called. How do I mock out the form.cleaned_data attribute if it doesn't exist in the first place? -
Flask Cache Clear on Refreshing the page
I need to clear the cache for proj1 template while refreshing the page as it loads image processing contents whereas home template has static images where I need to maintain cache. I have tried with below code but it doesn't work. Could anyone help me to improvise this model? cache_config = Cache(app, config={'CACHE_TYPE': 'null'}) app.config["CACHE_TYPE"] = "null" @app.route('/proj1/') def proj1(): return render_template('proj1.html') @app.route('/home/') def home(): return render_template('index.html') -
How to create abstract model in Django?
I am new to Django framework and in my project I have a model called Layer. class Layer(models.Model): name = models.CharField(max_length=255) I would like layer to have a relationship with an abstract model called "Geometry", more specifically, one layer should have one or no Geometry and one Geometry should be owned by one layer. The problem is that I have for types of Geometries and they all have different properties, so I decided to create multiple geometries: class Circle(models.Model): radius = models.CharField(max_length=255) class Rectangle(models.Model): height = models.CharField(max_length=255) width = models.CharField(max_length=255) I would like to have a data structure where both models are of the same type (Geometry). I would like to call layer.geometry and be able to get either a circle or a rectangle, or a cross and so on. Is that possible? And how is the database shape going to be like? Is Django going to create two different tables or one table with merged properties? Thanks in advance -
How to secure malicious PNG uploads on django?
I am hosting a Django application using Nginx which is serving media and static files on the same domain. In the Django docs, they warn about a vulnerability for malicious PNG uploads: Django’s media upload handling poses some vulnerabilities when that media is served in ways that do not follow security best practices. Specifically, an HTML file can be uploaded as an image if that file contains a valid PNG header followed by malicious HTML. This file will pass verification of the library that Django uses for ImageField image processing (Pillow). When this file is subsequently displayed to a user, it may be displayed as HTML depending on the type and configuration of your web server. Serving my static and media files on S3 for example is not an option in my case.. Also, I am not sure if I fully understand their second recommendation. It is recommended to white list files by its extensions, but what should keep out of my white list? .html or .png? In nginx.conf, I changed from default_type application/octet-stream; to default_type text/plain;. Is it safe enough? I just want to make sure that I will be able to serve static and media files by myself, … -
What is URL of my django deployed on gcp compute engine?
I am sorry to ask this beginner question but all results I can find is for Google App Engine deployment of Django and none for Google Compute Engine. Context I build a django app and tested locally. When I run it locally with "python manage.py runserver", it works when I enter "127.0.0.1:8000" it works. My Setup Now I bought a compute engine and created an instance. Installed my django app and now I run "python manage.py runserver" then it runs my website showing "Starting development server at http://127.0.0.1:8000/". But now I don't know what URL should I use to access my site over web!? My configuration for Google cloud compute: - Allowed HTTP/HTTPs while creating gcp instance - Added firewall ingress/egress for tcp 8000 Things I tried What URL should I now use to see my website over web? Say my external ip address is 35.243.41.52 , then I have already tried "35.243.41.52:8000" and "35.243.41.52/127.0.0.1:8000" but it doesn't shows my site. Then I tried to run "python manage.py runserver 35.243.41.52:8000" but it shows that this IP cant be assigned. It is my external ip of instance so it should be assigned? I think it should be a simple solution but … -
Understanding Database Locking during bulk_create() in Django
I'm using Django with Postgres as the DB. I have a table that has new entries getting created at a fairly high rate (average 4000 entries per minute). I'm now planning to move to a batch-write approach using the bulk_create() option. I'd like to understand if that's the correct thing to do. I'm worried about the database getting locked up in case the single batch write takes a long time, leaving other processes unable to read other entries from the table. Is this correct? Or is the lock a row-level lock, and the other entries are free to be read/updated? Also, if I should go ahead with it, how do I choose the correct batch size? TIA. -
Using setinterval to update Django Table
This post is based on a previous thread which seem to have a nice solution on how to reload table data in Django without refreshing the page. What it does: We use setInterval to make a GET AJAX request every 5 seconds to my view which renders a separate HTML file using this new context and then sends it as a response back to the main HTML table. It is working but it messes up the table structure and also creates duplicates. Issue 1: I do not use a table structure in the second HTML file, this messes it up since there is no styling once inserted into the main HTML file. But If I add a table structure the will be inserted for each row. The insertion also seem to insert all data from get_more_tables into the first column in the main HTML. Issue 2: It also creates duplicates, it keeps running despite only 2 rows. Is there any way to prevent this? Any help would be much appreciated! Main HTML & JS script <table class="table table-sm table-hover table-striped" id="_appendHere"> <thead class="thead-dark"> <tr> <th scope="col">Created</th> <th scope="col">Transaction</th> <th scope="col">Publisher</th> </tr> </thead> <tbody id="myBody"> {% for item in data %} … -
is it possible to target an object with a numeric variable and display it with the slug
I think it's useless to exclude the use of a numeric variable it has more flexibility and utility than a slug but a slug is better visually then all works well in my project but I would like to change that to be able to increase what is possible to do -
How to save Quill.js Rich text editor in Database MySQL, using Django?
I want to send the edited articles on my blog using the Quill Text Editor to the database and then retrieve the blog post when required. <div id="toolbar"></div> <div id="editor"></div> <button id="saveDraft">SAVE DRAFT</button> <script> var toolbarOptions = [ ['bold', 'italic', 'underline', 'strike'], // toggled buttons ['blockquote', 'code-block'], //[{ 'header': 1 }, { 'header': 2 }], // custom button values [{ 'list': 'ordered'}, { 'list': 'bullet' }], [{ 'script': 'sub'}, { 'script': 'super' }], // superscript/subscript [{ 'indent': '-1'}, { 'indent': '+1' }], // outdent/indent ['link', 'image','video','formula'], [{ 'direction': 'rtl' }], // text direction [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme [{ 'font': [] }], [{ 'align': [] }], ['clean'] ]; </script> <script> var quill= new Quill('#editor',{ modules:{ toolbar:toolbarOptions }, theme: 'snow' }); $('#saveDraft').click(function () { var draft = quill.getContents(); console.log(draft); }); Above code puts the editor on the page and last 2 lines saves the data in certain variable. From some searching i got found that data is JSON. The pictures below shows the variable it store data in. With Addition of New … -
Django Filter Choice Field displays correct values but wont filter
I'm using the Django filter app: https://github.com/carltongibson/django-filter I'm making a web-comic app and what I'm trying to accomplish is a drop-down filter field that displays the distinct values for that field. In this case, the series field. After reading their docs. And stack overflow questions like this one or this one . I can make the filter form display the correct drop down values. But when I attempt to filter by them, my result queryset returns no values! There must be something in the docs I'm not understanding. Here's my hopefully relevant code. filters.py import django_filters from .models import ComicPanel # Grab Distinct values for series def getUniqueSeries(): series_dicts = ComicPanel.objects.all().values_list('series').distinct() series_list = [] i = 0 for item in series_dicts: series_list.append((i,item[0])) i = i+1 return series_list class ComicPanelFilter(django_filters.FilterSet): series = django_filters.ChoiceFilter(choices = getUniqueSeries()) class Meta: model = ComicPanel fields = ['chapter', 'episode'] views.py def view_archive(request): comic_list = ComicPanel.objects.all() comic_filter = ComicPanelFilter(request.GET, queryset=comic_list) paginator = Paginator(comic_filter.qs, 8) page = request.GET.get('page', 1) try: comics = paginator.page(page) except (PageNotAnInteger, TypeError): comics = paginator.page(1) except EmptyPage: comics = paginator.page(paginator.num_pages) return render(request, 'comics/comic_archive.html', context = {'filter': comic_filter, 'comics': comics}) template.html ... <form class="form" method="GET"> {% csrf_token %} <table class="my_classes"> <thead> <tr> <th scope="col">Filter Comics: … -
Enter a valid date/time in Django Forms
Anytime I try to input datetime into my form, it always says Enter a valid date/time I tried different datetime formats; the one i speified and the default ones, they all did not work. I have tried different solutions on Stack Overflow, but they all do not seem to work. The field I have this issue with is due_date NOTE: I didn't show any imports Also, i need recommendations for nice datetime pickers. All the ones I tried didn't work for some reason or the other. views.py: @login_required def task_create(request): data = dict() #, data=request.POST if request.method == 'POST': form = TaskForm(request.user, data=request.POST) if form.is_valid(): form.save() data['form_is_valid'] = True else: data['form_is_valid'] = False else: form = TaskForm(request.user) context = {'form': form} data['html_form'] = render_to_string('partial_task_create.html', context, request=request ) return JsonResponse(data) forms.py: class TaskForm(ModelForm): due_date = forms.DateTimeField(input_formats=['%I:%M %p %d-%b-%Y']) parent = forms.ModelChoiceField(queryset=Task.objects.all(), empty_label="---None---", required=False) #widget=DateTimePickerInput(format='%I:%M %p %d-%m-%Y') class Meta: model = Task fields = ['task_title', 'task_description', 'due_date', 'is_completed', 'categories', 'parent'] widgets = { #'due_date': DateTimeInput() } def __init__(self, user, *args, **kwargs): super(TaskForm, self).__init__(*args, **kwargs) self.fields['categories'].queryset = Categories.objects.filter(Q(user_id__isnull=True) | Q(user_id=user.id)) -
Python Can't Find Correct Mysqlclient
I'm trying to get mysql up on a new Django project on MacOS. I used pip to intsall it. This is the output from my second attempt so that I could show the install here: python3 -m pip install mysqlclient Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: mysqlclient in /Users/curt/Library/Python/3.7/lib/python/site-packages (1.4.6) This is my init.py file: (the stack overflow editor has dropped the underscores) import mysqlclient mysqlclient.install_as_MySQLdb() PyCharm is indicating it can't find mysqlclient on line 1 and the message "Package requirement 'mysqlclient==1.4.6' is not satisfied" is displayed. I tried adding the path shown in the pip install to $PYTONPATH to no effect. Is there somewhere else I should to be setting the location of mysqlclient? -
Retrive value list of Decimal Type without class name
when I run this query in django: list(InputDataDetailItem.objects.filter(wage_detail__wage=wage).values_list('value',flat=True)) I get : [Decimal('30.00'), Decimal('0.00'), Decimal('0.00')] but I want : [30.00,0.00,0.00] How can I do this? Thank you. -
i have list of slices of DICOM image in views.py and i want to view this image in the template using Django
i'm using function in views.py that generates list of slices of DICOM image and i need away to show this image in the front end of Django app , in any python script i can show image by matplotliiip library using for each slice : plt.imshow(slice) plt.show -
'NoneType' object is not callable in Django App
I have made a product model in django. I'm able to insert products, but now i'hanving problems to get the products. When i list the products, i get all items. But when i try get only one, the restframework is giving me this error: I'm trying to acessing the product by using this url http://localhost:4444/products/1 TypeError at /products/1 'NoneType' object is not callable Request Method: GET Request URL: http://localhost:4444/products/1 Django Version: 2.2.8 Exception Type: TypeError Exception Value: 'NoneType' object is not callable Exception Location: C:\Users\vini\Documents\VirtualEnv\mystore\lib\site-packages\rest_framework\generics.py in get_serializer, line 110 Python Executable: C:\Users\vini\Documents\VirtualEnv\mystore\Scripts\python.exe Python Version: 3.8.0 Python Path: ['C:\\Users\\vini\\Documents\\GitHub\\mystore-backend', 'C:\\Users\\vini\\Documents\\VirtualEnv\\mystore\\Scripts\\python38.zip', 'C:\\Users\\vini\\Documents\\VirtualEnv\\mystore\\DLLs', 'C:\\Users\\vini\\Documents\\VirtualEnv\\mystore\\lib', 'C:\\Users\\vini\\Documents\\VirtualEnv\\mystore\\Scripts', 'c:\\users\\vini\\appdata\\local\\programs\\python\\python38\\Lib', 'c:\\users\\vini\\appdata\\local\\programs\\python\\python38\\DLLs', 'C:\\Users\\vini\\Documents\\VirtualEnv\\mystore', 'C:\\Users\\vini\\Documents\\VirtualEnv\\mystore\\lib\\site-packages'] Server time: Sab, 15 Fev 2020 19:41:00 +0000 Model: class Product(UGCModel): class Meta: app_label = 'product' avatar = models.ForeignKey('image.Image', related_name='product_image', on_delete=models.CASCADE) # Variable to hide in store hide = models.BooleanField(default=False) # Value of product value = models.DecimalField(max_digits=6, decimal_places=2, validators=[MinValueValidator(0)]) # Name of the product title = models.CharField(max_length=150, null=False, blank=False) # Quantity of product in stock quantity = models.IntegerField(default=0, blank=False, null=False, validators=[MinValueValidator(0)]) # Discount of the product discount = models.IntegerField(default=0, validators=[MaxValueValidator(100), MinValueValidator(0)]) # description of the product resume = models.CharField(max_length=1000, blank=True) def __str__(self): return '(Product)%s' % self.uid Url: from django.urls import path from . import … -
Django — database connections are not closed after async tests
I use Django ORM inside async code. Everything works fine and all tests pass. However, DB connections do not close properly in tests. Here is an example: from asgiref.sync import sync_to_async, async_to_sync @sync_to_async def count_books(): return Book.objects.count() class FooTest(TestCase): def setUp(self): Book.objects.create(title='Haha') def test1(self): import asyncio c = asyncio.run(count_books()) self.assertEqual(1, c) def test2(self): c = async_to_sync(count_books)() self.assertEqual(1, c) Postgres error: django.db.utils.OperationalError: database "test_mydbname" is being accessed by other users Sqlite error: sqlite3.OperationalError: database table is locked: test_mydbname I've tried swapping sync_to_async with database_sync_to_async from django-channels, but this didn't change anything. How can I fix this? -
Need Advice on building Search Filters in Django Product Search Page
I have a product search page that lists all cars. I would like to know some best practices in order to achieve searching and filtering results like in this picture below. At present, I have a working code that does the job fine. But I feel am hacking instead of following the best practice. Do I need to learn Node.js to achieve this ? or Django/CSS is enough? What is the industry's best practice? -
Adding class for django_filters return an exception " django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet."
After installing django_filters and adding a filter class return an exception. " django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet." """ class ProductFilter(django_filters.FilterSet): class Meta: model = product fields = ['price', 'category', 'location', 'tags', 'pub_date'] """ -
Combining two separate forms in one Django view?
This question have been answered before, e.g here: Proper way to handle multiple forms on one page in Django So before it gets marked as a duplicate. I'll try to explain why its different. I've got three tables, Project, ProjectUser and User. ProjectUser is a join table to indicate what users belongs to what project. I'm trying to create a view that lets users update project details (e.g. name of project), and also add users to the project (which is indicated by a dropdown that shows all available users like the standard one for models with foreign keys in the django admin panel). All works fine until I'm trying to pass an id from the views to the formclass and submit. views.py class ProjectUpdateView(UpdateView): form_class = ProjectUpdateForm second_form_class = ProjectUserAddForm template_name = 'projects/project_edit.html' success_url = reverse_lazy('projects:list') def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) id_ = self.kwargs.get("id") project = Project.objects.get(id=id_) if 'form' not in context: context['form'] = self.form_class() if 'form2' not in context: team = Organization.objects.get(id=project.organization_id) context['form2'] = self.second_form_class(queryset=team) # <-- here is where I wish to pass a queryset, which fails when trying to submit form2. context['project_users'] = ProjectUser.objects.filter(project__id=project.id).select_related("project") context['team'] = Organization.objects.get(id=project.organization_id) return context def get_object(self): id_ = self.kwargs.get("id") return get_object_or_404(Project, … -
sites_query = connection.execute("SELECT domain FROM django_site") psycopg2 has no attribute execute
When I try to start gunicorn, I am getting this error: File "/home/django-project/projectfolder/settings.py", line 270, in ALLOWED_HOSTS = get_allowed_hosts(DATABASES['default']) File "/home/django-project/projectfolder/allowed_hosts.py", line 16, in get_allowed_hosts sites_query = connection.execute("SELECT domain FROM django_site") AttributeError: 'psycopg2.extensions.connection' object has no attribute 'execute from .settings import * DEBUG = False DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'projectname_settings', 'USER': '******', 'PASSWORD': '******', 'HOST': 'localhost', 'PORT': '', } } ALLOWED_HOSTS = [ "mydomain.com", ] + get_allowed_hosts(DATABASES['default']) Allowed_hosts.py def get_allowed_hosts(db_params): connection = None if db_params['ENGINE'] == 'django.db.backends.postgresql_psycopg2': import psycopg2 connection = psycopg2.connect(user=db_params['USER'], password=db_params['PASSWORD'], host=db_params['HOST'], port=db_params['PORT'], database=db_params['NAME']) elif db_params['ENGINE'] == 'django.db.backends.sqlite3': import sqlite3 connection = sqlite3.connect(db_params['NAME']) if connection is not None: sites_query = connection.execute("SELECT domain FROM django_site") sites_result = sites_query.fetchall() sites = ["." + site[0] for site in sites_result] print("Allowed hosts") print(sites) return sites -
How to remove unused CSS from plain Django project that use Bootstrap?
How to use Purgecss in plain Django project that uses Bootstrap? No webpack etc. https://github.com/FullHuman/purgecss -
How to use discord.py library to get members in a voice channel and then display in my django site?
import discord import random from discord.ext import commands client = commands.Bot(command_prefix = '.') channel = None @client.event async def on_ready(): channel = client.get_channel(677999369642836037) async def members_list(request): curMembers = [] for member in channel.members: curMembers.append(member) return render(request, "discordTool/discordTool.html", { 'members_list': curMembers, }) client.run('my token') This is my views.py of an app that supposes to show active members in a voice channel in my django site. However this doesn't work, can anyone help me? -
Footer element keeps shrink in Django templates
I have included the bootstrap footer in to my django base template (base.html) along with pagination. When I run the django server the footer appear shrink towards the left( [1]: https://i.stack.imgur.com/1XyKN.png), then when clicks on the pagination, the footer appear normal with full width (). Base.html {% load static%} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block head_title %} Blog {% endblock head_title %}</title> <link rel="stylesheet" href='{%static "css/base.css"%}'> <style> {% block style %} #main_nav { background-color: white; box-shadow: 5px 10px 20px -20px rgba(85, 172, 238, 1); margin-left: auto; margin-right: auto; } .navbar-toggler { color: #1c2331; } #Create_Post { border-radius: 22.5px; background-color: #ffffff; border-color: black; border-width: 2px; } #Create_Post:hover { border-color: #007bff; } #tag_button { border-radius: 22.5px; background-color: #ffffff; border-color: black; border-width: 2px; } #tag_button:hover { border-color: #007bff; } #no_results { margin-left: auto; margin-right: auto; } #pagination { margin-left: auto; margin-right: auto; } #pagination-link { font-size: 15px; padding: 6px 9px; margin-right: 6px; margin-left: 6px; -webkit-border-radius: 50%; -moz-border-radius: 50%; border-radius: 50%; width: 50px; height: 50px; border-style: solid; border-color: black; border-width: 2px; color: black; text-decoration: none; } #pagination-link:hover { border-color: #007bff; color: #007bff; } #main_footer{ background: linear-gradient(135deg,#2a99ef 20%,#52d9e5 80%); width:1287px; } {% endblock style %} </style> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous"> </head> … -
How to display an empty form and form with saved data from DB on same page using Ajax, Jquery in Django
I have created a two models Template and TempData in django, where Template stores the title of template in Template model which is related to the TempData where we can store n items. The respected modelforms for the above models are below (Template is ForeignKey to TempData) class TemplateForm(forms.ModelForm): title = forms.CharField(label='', widget=forms.TextInput(attrs={'onblur':'updateTitle()','placeholder':'Title','class':'input-no-border', 'id':'js_template_title', 'alt':'rename'})) class Meta: model = Template fields = ['title'] class TemplateDataForm(forms.ModelForm): item = forms.CharField(widget=forms.TextInput(attrs={'id':'js_temp_item'})) status = forms.ChoiceField(label='', choices=CHOICES, widget=forms.Select(attrs={'id':'status', 'class':'btn-switch switch-select switch-select-button switch-select-button:hover'})) class Meta: model = TempData fields = ['item','status'] I'm able to save the Template 'title' and related n 'items' data to DB using Ajax and Jquery, but I'm not able to display list of TempDataForm with the saved data in DB using Ajax JQuery Facing Serializing issue I'm not getting any idea in displaying the form with saved data other than the one which I have followed in this question, can you suggest me a way to do this Main objective of displaying the form with save data is allow users to update the data and choices -
How do I make media files display automatically in django using s3 as storage?
I configured my s3 settings like so: DEFAULT_FILE_STORAGE = 'Bonychicken.storage_backends.MediaStorage' AWS_STORAGE_BUCKET_NAME = 'bonychicken' AWS_S3_REGION_NAME = 'us-east-2' AWS_ACCESS_KEY_ID = '<my access key id>' AWS_SECRET_ACCESS_KEY = '<my secret access key>' AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com" AWS_QUERYSTRING_AUTH = False AWS_DEFAULT_ACL = None STATIC_URL = '/static/' MEDIAFILES_LOCATION = 'media' MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/media/" MEDIA_ROOT = f"https://{AWS_S3_CUSTOM_DOMAIN}/media/" AWS_IS_GZIPPED = True AWS_S3_OBJECT_PARAMETERS = { 'CacheControl': 'max-age=86400', } My storage class is: from storages.backends.s3boto3 import S3Boto3Storage class MediaStorage(S3Boto3Storage): bucket_name = 'bonychicken' location = 'media' file_overwrite = False I added storages to my installed apps and boto3 is also installed. CORS Configuration is: <?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <CORSRule> <AllowedOrigin>*</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> <MaxAgeSeconds>3000</MaxAgeSeconds> <AllowedHeader>Authorization</AllowedHeader> </CORSRule> </CORSConfiguration> block public access has been turned off. IAM user has permissions to the bucket. The problem is, whenever I upload a file, it is uploaded to my s3 bucket but it doesn't show on the html page. Trying to access the image url from the source code yields: <Error> <Code>AccessDenied</Code> <Message>Access Denied</Message> <RequestId><random letters here></RequestId> <HostId> <random letters here> </HostId> </Error> But if I go to my bucket settings and select actions and click on make public after marking the media files, everything starts working. This means for each image uploaded, I have to mark and …