Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
how to keep selected option after refresh the page
I trying to implement filter option with django I can do the filtering, but can't keep the selected option after the refresh because I am rendering the filtered result, any solution? views.py def filter(request): products = Product.objects.all() print(products) if request.method == 'POST': title = request.POST.get('da',None) print(title) titre = Product.objects.filter(title=title) print(titre) return render(request, 'searchapp/searchview.html', {'qs': titre,'ds':products}) return render(request,'searchapp/searchview.html',{'qs':products}) html : <div> <form action="{% url 'search:query' %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <div class="form-group"> <label>Title</label> <select name="da" class="form-control"> {% for obj in qs %} <option value="{{obj}}">{{obj}}</option> {%endfor%} <!-- <input type="text" class="form-control" name="title">--> </select> </div> <button type="submit" class="btn btn-warning btn-lg">Add Product</button> </form> </div> -
Make a Django query from raw SQL with Distinct over Django ManyToMany Relation
models.py class FlagsModel(models.Model): name = models.CharField(max_length=64, verbose_name='Flag') class ColorsModel(models.Model): name = models.CharField(max_length=64, verbose_name='Color') class RelationModel(models.Model): flag = models.ForeignKey(FlagsModel, null=True, on_delete=models.SET_NULL) color = models.ForeignKey(ColorsModel, null=True, on_delete=models.SET_NULL) I am looking now for a Query which selects each flag containing the colors "yellow" and "red" in raw SQL i can do something like this: SELECT DISTINCT a1.flag_id from relationmodel a1, relationmodel a2 where a1.color_id=2 AND a2.color_id=6 (id 2 = yellow, id 6= red) but i havent found a solution that's gonna work in django any ideas ? -
Avoid duplicates in postgresql database after scraping with beautifull soup
I'm scraping god names from the website of a game. The scraped text is stored in a postgresql database through Django models. When I run my program twice, I get everything double. How do I avoid this? import requests import urllib3 from bs4 import BeautifulSoup import psycopg2 import os import django os.environ['DJANGO_SETTINGS_MODULE'] = 'locallibrary.settings' django.setup() from scraper.models import GodList urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) session = requests.Session() session.headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"} url = 'https://www.smitegame.com/' content = session.get(url, verify=False).content soup = BeautifulSoup(content, "html.parser") allgods = soup.find_all('div', {'class': 'god'}) allitem = [] for god in allgods: godName = god.find('p') godFoto = god.find('img').get('src') allitem.append((godName, godFoto)) GodList.objects.create(godName=godName.text) -
manage.py startapp creates read-only files from within docker
I have a django app running in a docker container. I bashed into the container and used python manage.py startapp. However, the created directories and files appear to be read-only. From within intellij, I am unable to edit the files. I get a popup offering to clear the readonly status, but that just gives me an error unable to clear readonly status on the following files. How can I edit my files outside of the container? -
DjangoRestAPI : "detail": "Unsupported media type \"text/plain\" in request."
Postman throwing me error while i request the url as 'POST' method class ProductAddAPIView(APIView): def post(self,request,format="text/plain"): product_data = [] title = request.POST.get('title') slug = request.POST.get('slug') # price = request.POST.get('price') all_data = [title, slug] product_data.append(all_data) return Response({"product_data":product_data},status=status.HTTP_200_OK) -
flake8 internal error in regular expression engine
I'm trying to run flake8 on a docker django built like described here (tutorial page) when building the docker image I get an error from flake8 which is run in an docker-compose file with like so $ flake8 --ignore=E501,F401 . multiprocessing.pool.RemoteTraceback: Traceback (most recent call last): File "/usr/local/lib/python3.8/multiprocessing/pool.py", line 125, in worker result = (True, func(*args, ** File "/usr/local/lib/python3.8/multiprocessing/pool.py", line 48, in return list(map(* File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 666, in return checker.run_checks() File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 598, in run_checks self.run_ast_checks() File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 495, in run_ast_checks checker = self.run_check(plugin, tree=ast) File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 426, in run_check self.processor.keyword_arguments_for( File "/usr/local/lib/python3.8/site-packages/flake8/processor.py", line 241, in keyword_arguments_for arguments[param] = getattr(self, param) File "/usr/local/lib/python3.8/site-packages/flake8/processor.py", line 119, in file_tokens self._file_tokens = list( File "/usr/local/lib/python3.8/tokenize.py", line 525, in _tokenize pseudomatch = _compile(PseudoToken).match(line, pos) RuntimeError: internal error in regular expression engine The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/local/bin/flake8", line 8, in <module> sys.exit(main()) File "/usr/local/lib/python3.8/site-packages/flake8/main/cli.py", line 18, in main app.run(argv) File "/usr/local/lib/python3.8/site-packages/flake8/main/application.py", line 393, in run self._run(argv) File "/usr/local/lib/python3.8/site-packages/flake8/main/application.py", line 381, in _run self.run_checks() File "/usr/local/lib/python3.8/site-packages/flake8/main/application.py", line 300, in run_checks self.file_checker_manager.run() File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 329, in run self.run_parallel() File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 293, in run_parallel for ret in pool_map: File … -
Perform the PHP operation in Python in Django
I am calling a php file from a javascript code in Django, it seems it is not able to find it. So i was thinking to call a python function that will perform the same operation as of the given in PHP. the js code where im calling php file var filename = new Date().toISOString(); //filename to send to server without extension //upload link var upload = document.createElement('a'); upload.href = "#"; upload.innerHTML = "Upload"; upload.addEventListener("click", function(event) { var xhr = new XMLHttpRequest(); xhr.onload = function(e) { if (this.readyState === 4) { console.log("Server returned: ", e.target.responseText); } }; var fd = new FormData(); fd.append("audio_data", blob, filename); xhr.open("POST", "upload.php", true); xhr.send(fd); }) li.appendChild(document.createTextNode(" ")) //add a space in between li.appendChild(upload) //add the upload link to li Upload.php print_r($_FILES); //this will print out the received name, temp name, type, size, etc. $input = $_FILES['audio_data']['tmp_name']; //get the temporary name that PHP gave to the uploaded file $output = $_FILES['audio_data']['name'].".wav"; //letting the client control the filename is a rather bad idea //move the file from temp name to local folder using $output name move_uploaded_file($input, $output) so basically this php file is setting the blob file in my server Can anyone please help me to convert … -
Serving static files while debug = false, and debug = true
I have django project running, 2.2. And python version 3.6.9 If i have debug = True, everything is running correctly. But when I have debug = False, my css gets ruined. It has to do with staticfiles i guess. I want to know, if am running apache server using pycharm on ubuntu. Where do I need to copy/paste my static and static_deploy folders for this to work? -
Django Annotated Query to Count Only Latest from Reverse Relationship
Problem Overview Given the models class Candidate(BaseModel): name = models.CharField(max_length=128) class Status(BaseModel): name = models.CharField(max_length=128) class StatusChange(BaseModel): candidate = models.ForeignKey("Candidate", related_name="status_changes") status = models.ForeignKey("Status", related_name="status_changes") create_id = models.DateTimeField(auto_now_add=True, blank=True) And SQL Tables: candidates +----+--------------+ | id | name | +----+--------------+ | 1 | Beth | | 2 | Mark | | 3 | Mike | | 4 | Ryan | +----+--------------+ status +----+--------------+ | id | name | +----+--------------+ | 1 | Review | | 2 | Accepted | | 3 | Rejected | +----+--------------+ status_change +----+--------------+-----------+------------+ | id | candidate_id | status_id | created_at | +----+--------------+-----------+------------+ | 1 | 1 | 1 | 03-01-2019 | | 2 | 1 | 2 | 05-01-2019 | | 4 | 2 | 1 | 01-01-2019 | | 5 | 3 | 1 | 01-01-2019 | | 6 | 4 | 3 | 01-01-2019 | +----+--------------+-----------+------------+ I want to get the get the total number of candidates with a given status, but only the latest status_change is counted. SQL Solution Using SQL, I was able to achieve it using Group BY and COUNT. (SQL untested) SELECT status.id as status_id , status.name as status_name , COUNT(*) as status_count FROM ( SELECT status_id, Max(created_at) AS … -
create api for url shortning in Django
My model: class UrlShort(models.Model): hash_url = models.CharField(max_length=100) original_url = models.CharField(max_length=100) creation_date = models.DateTimeField() expiry_date = models.DateTimeField() I need to create a hashed url (from original url) with 16 byte in django and store in database with expiry time of 30 seconds of creation time. What will be Query ? -
Django and Excel File
I have an unusual question. I made a website that Scrapes another website and store/organizes all the data on multiple spreadsheets (XLSX Excel file). These excel files are stored on the computer running the website. Every time the scrape button is clicked, the website is scraped and the data is stored. What I want to do, is to make that data available. So, after the website is scraped I want the excel files to be download on the computer that used my website. How can I do that, is there an easy way? -
how to get data from selection tag with django
I trying to implement filter function to get data from a selection tag using post form but the result is always None view.py div> <form action="{% url 'search:query' %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <div class="form-group"> <label>Title</label> <select title="da" class="form-control"> {% for obj in qs %} <option value="{{obj}}">{{obj}}</option> {%endfor%} <!-- <input type="text" class="form-control" name="title">--> </select> </div> <button type="submit" class="btn btn-warning btn-lg">Add Product</button> </form> </div> html : div> <form action="{% url 'search:query' %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <div class="form-group"> <label>Title</label> <select title="da" class="form-control"> {% for obj in qs %} <option value="{{obj}}">{{obj}}</option> {%endfor%} </select> </div> <button type="submit" class="btn btn-warning btn-lg">Add Product</button> </form> </div> -
Different newline character lengths for Django and SNS servers, while subscribing a server Endpoint to SNS
I am currently writing an API which will recieve direct messages (POST) from AWS Simple Notification Sercive. I am currently facing the issue with the content-length of the message sent by SNS to my end point. Below is the payload : { "Type" : "Notification", "MessageId" : "da41e39f-ea4d-435a-b922-c6aae3915ebe", "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic", "Subject" : "test", "Message" : "test message", "Timestamp" : "2012-04-25T21:49:25.719Z", "SignatureVersion" : "1", "Signature" : "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=", "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem", "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55" } And the headers are: x-amz-sns-message-type: Notification x-amz-sns-message-id: da41e39f-ea4d-435a-b922-c6aae3915ebe x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55 Content-Length: 761 Content-Type: text/plain; charset=UTF-8 Host: ec2-50-17-44-49.compute-1.amazonaws.com Connection: Keep-Alive User-Agent: Amazon Simple Notification Service Agent The payload above can have two content-lengths : 761 and 773. The issue is SNS counts newline as one character('\n') while the django server counts the newline as two characters('\r\n'). SNS sends content-length to be 761, and hence django closes the stream after reading the 761's character according to it. But as it considers newline to be two characters, it leaves some information. Also, the point to notice is that SNS sends json with header of content-type set to 'text/plain' and not 'application/json'. What should I do to parse the whole message, and not leave even a single character … -
Django form not showing fields in correct order
I am not sure what i am doing wrong. The fields are mentioned in order. Still i get different order of the fields everytime i restart the django server. Here is the screenshot. views.py def user_creation(request): if request.method == 'POST': user_form = UserRegForm(request.POST) if user_form.is_valid(): user = user_form.save() return render(request, 'info/base.html') else: user_form = UserRegForm() context = { 'UserForm' : user_form, } return render(request, 'users/form.html', context ) forms.py from django import forms from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm class UserRegForm(UserCreationForm): class Meta: model = User fields = { 'username', 'password1','password2' } -
How or where to handle form validation errors for unique constraint in modelformset_factory formset
I'm using a modelformset_factory formset and I'm not sure where and/or how to handle unique constraint errors so that I can gracefully display them on my template html instead of just crashing the app. It seems that when you create a formset in the forms.py that you can't then create a function underneath and start trying to clean data and/or raise form validation errors. The formset works great in adding new data but at this point, I'm not sure how to handle the unique constraint errors. Model class Disciplines(models.Model): class Meta: unique_together = (('account','discipline'),) verbose_name_plural = ('Disciplines') account = models.ForeignKey(Account, on_delete=models.CASCADE) discipline = models.CharField(max_length=40,choices=Discipline_Choices) rank = models.CharField(max_length=41, choices=Rank_Choices) conferred_date = models.DateField(blank=True, null=True) expires_date = models.DateField(blank=True, null=True) def __str__(self): return self.account.first_name + ' ' + self.account.last_name + ' ' + self.discipline + ' ' + self.rank def has_module_perms(self, app_label): return True Form from django.forms.models import modelformset_factory from account.models import Disciplines DisciplineFormSet = modelformset_factory(Disciplines, fields=('id','discipline','rank'), can_delete=True) View def discipline_update_view(request): if not request.user.is_authenticated: return redirect('login') user = request.user account_id = user.id if request.POST: Disc_Update_Form = DisciplineFormSet(request.POST) # import pdb; pdb.set_trace() if Disc_Update_Form.is_valid(): for form in Disc_Update_Form: instance = form.save(commit=False) if not instance.discipline: continue instance.account_id = account_id instance.save() return redirect('account') else: Disc_Update_Form = DisciplineFormSet(queryset=Disciplines.objects.filter(account_id=account_id).order_by('rank')) … -
Set a value in Model field based on another value from other model
I have two models where one has a ForeignKey relationship with other entity. The thing is the date in data_start field from ModelB needs to be not equal and grater than the date in date_end from ModelA. How can I do this comparison(rule) inside the ModelB and save it? class ModelB(models.Model): date_start = models.DateTimeField('') date_end = models.DateTimeField('') class ModelA(models.Model): name = models.CharField(...) date_start = models.DateTimeField('') date_end = models.DateTimeField('') resource = models.ForeignKey(ModelB,...) -
django.db.utils.IntegrityError: NOT NULL constraint failed: users_customuser.user_type
I am trying to create a role-based authentication in Django and i getting this error django.db.utils.IntegrityError: NOT NULL constraint failed: users_customuser.user_type This is my models: from django.contrib.auth.models import AbstractUser from django.db import models class CustomUser(AbstractUser): USER_TYPE_CHOICES = ((3, 'secretary'),(4, 'supervisor'),(5, 'admin'),) user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES) def __str__(self): return self.name -
The model has two many-to-many relations through the intermediate model
I have a model that has 2 separate ManyToManyField relations back to itself class Company(models.Model): parent = models.ManyToManyField("self", through='CompanyParent', through_fields=('company_child', 'company_parent'), related_name='+') child = models.ManyToManyField("self", through='CompanyParent', through_fields=('company_parent', 'company_child'), related_name='+') The above works fine on my localhost Django v3.0.2/ SQLite 3.8.7.2 To actually publish it, I have had to use Django v2.1.15/ SQLite 3.7.17, but with the published version it is chucking out the following errors companies.Company.child: (fields.E332) Many-to-many fields with intermediate tables must not be symmetrical. companies.Company.parent: (fields.E332) Many-to-many fields with intermediate tables must not be symmetrical. companies.Company: (models.E003) The model has two many-to-many relations through the intermediate model 'companies.CompanyParent'. What's going on here? Solved the first 2 issues by adding symmetrical=False to each model, but no idea how to solve the final error? -
How to insert values into related models?
I have two models as follows: class Reservation(models.Model): startDate = models.DateField() endDate = models.DateField() count = models.IntegerField(blank=False, null=False) def generateReservationItems(self): for d in range((endDate - startDate).days): reservationItem= ReservationItems(reservationDate=d, count = count) reservationItem.save() class ReservationItems(models.Model): reservationDate = models.DateField(); reservation = models.ForeignKey(Reservation, on_delete=models.CASCADE) count = models.IntegerField(blank=False, null=False) class Reservation represents a package of dates (range of dates), and the class ReservationItems stores every single date exists in Reservation class. I built 'generateReservationItems()'function to be automatically invoked when a Reservation object is saved. The function inserts number of items equals to the different between startDate and endDate. for example: if startDate = Mars 6 2020, and endDate = Mars 10 2020, 5 items will be inserted in ReservationItems: Mars 6 2020 Mars 7 2020 Mars 8 2020 Mars 9 2020 Mars 10 2020 But Reservation objects are inserted successfully without inserting the items into reservationItems. I don't know how to make the 'generateReservationItems()' invoked once a reservation object is created. any ideas please to do it, or if there and other effective methods to perform such tasks. -
Calling a function from a python file into a html template in Django
I am trying to add the output of a function from a python file into an html file in django project. function name is stat() and the file name is test1.py: from django import template register = template.Library() @register.simple_tag def stat(): x = 'Succccceeesss' return x html file name is html_test1.html: {% load test1 %} <div id="id_div"> {% stat as result %} <h> {{ result }} </h> </div> I know I can build the html string in the python file and then call it in the view, but this defeats the purpose of using django, and is not a clean/efficient way to handle enormous quantity of data. Thanks. -
Href with ManyToMany category slug
My Product model has a ManyToManyField Category attribute which I'm having trouble including in my url. I don't know how it will decide which category to use for the category_slug since there are multiple categories or some products. models.py class Product(models.Model): def image_dir(self, filename): modeldir = slugify(self.title) return "models/" + osjoin(modeldir, filename) title = models.CharField(max_length=80) featured_image = models.ImageField(upload_to=image_dir) category = models.ManyToManyField(Category) model_slug = AutoSlugField(null=True, default=None, unique=True, populate_from='title') class Meta: verbose_name_plural = "Products" def __str__(self): return self.title template.html {% for model in category.product_set.all %} <div class="col-md-3 col-sm-6 d-flex align-items-stretch p-0"> <a href="{% url 'main:model_detail_view' model.category.category_slug model.model_slug %}"> <div class="categories-model-card p-3 m-3 animated fadeIn "> <img src="{{model.featured_image.url}}" class="img-fluid d-block"> <p class="model-name m-0 text-center">{{model.title|truncatechars:35}}</p> <div class="row"> <div class="model-title-spacer mt-2 mb-2"></div> </div> </div> </a> </div> {% endfor %} </div> {% endfor %} -
How to create default django permissions for un-managed models?
I have prepopulated database and un-managed model that looks like this: class Venue(models.Model): name = models.CharField(max_length=255) ... class Meta: managed = False db_table = 'venue' How can I create default permissions for this model (eg change_venue, view_venue, etc)? I tried to use django.contrib.auth.management.create_permissions(self) inside AppConfig.ready but it will only work if default django models (eg ContentType) are already created. Also tried to specify permissions in the Meta class but django throws an error "attempt to override default permissions". I have some thoughts about using migrations, but is it safe to use create_permissions functions (and similar) in migration for un-managed table (I usually write raw sql for them)? -
Django IIS and SQL with NT Authentication
Good morning. I have several Django apps running in my work environment. All are running through Gunicorn on Ubuntu. I have a new app that must run on Windows for ODBC reasons. This new app is running in development mode on a Windows 10 PC. I run the dev server while logged in as a service account, it uses a trusted connection (local credentials as I understand) to query a SQL server and works perfectly. I ported this app (virtualenv and all) to a Windows Server 2012 r2 instance and run the dev server successfully from there using my domain admin credentials (again trusted_connection=yes). I set up IIS on this server to run the app permanently and it appears to run. However, I get a django error page that says the SQL login failed for 'DOMAINNAME\SERVERNAME'. I have tried editing my Application Pool to use the credentials of the working service account (Advanced Settings > Process Model > Identity). With this configuration I no longer see a Django error page, just Service Unavailable HTTP Error 503. I have tried 'recycling' and restarting the IIS service after the change but that has not worked. Unfortunately, I do not have admin access … -
How to get samde data from model twice in html - Django?
i have slider of images with some text ! So basically I want when I click on image to pop up dialog! I have models and from there is that picture.So when it pop up I want same data there like "Name of image" "Desc about it" and so on.I have 7 images ! How can i do that ??? -
Cannot upload data through a foreign keys with Crispy Forms
I display the values of categories and subcategories, getting the value from there and save it as a foreign key in the News entity. models.py class News(models.Model): name = models.CharField(max_length=50) pic = models.ImageField() catid = models.ForeignKey(SubCat, on_delete = models.DO_NOTHING) class Cat(models.Model): name = models.CharField(max_length=50) class SubCat(models.Model): name = models.CharField(max_length=50) catid = models.ForeignKey(Cat, on_delete = models.CASCADE) add_news.html <label class="col-md-1 control-label" for="example-text-input">Text Input</label> <div class="col-md-5"> <input id="newstitle" name="newstitle" type="text" class="form-control" placeholder=".col-md-7"> </div> <select id="newscat" name="newscat" class="select-chosen" data-placeholder="Choose a Category.." style="width: 250px;"> {% for i in subcat %} <option value="{{ i.pk }}">{{ i.catid.name }} | {{ i.name }}</option> {% endfor %} </select> image Then I got all the values using queries and saved them like this: views.py @login_required(login_url='mylogin') def news_add(request): subCat = SubCat.objects.all() catID = request.POST['newscat'] if request.method == 'POST': newstitle = request.POST['newstitle'] newCatID = SubCat.objects.get(pk=catID) pic= request.FILES['picture'] news = News(name=newstitle, catid=newCatID, pic=pic) ... news.save() else: return render(request, 'back/news_add.html', {'subcat' : subCat}) But, now how can I use the crispy forms to display the same list of categories with subcategories and save it in my model PK (catid)? So, I can only display this add_news.html <div class="col-md-5"> {{ form.name|as_crispy_field }} </div> <div class="col-md-5"> {{ form.pic|as_crispy_field }} </div> <div class="col-md-5"> {{ form.catid.name|as_crispy_field }} {{ …