django

Django: filter a model with a field from another model

浪子不回头ぞ 提交于 2021-02-11 17:47:41
问题 This is my first Django app. I am trying to display an average rating from multiple reviews. This seems to work in my product template but not on my blog template because productprofile_id is not defined: product_rating = Review.objects.filter(product_id=productprofile_id).aggregate(Avg('rating')).values() How do I filter the Review model with the related_products field in the Blog model? Or is it possible to just include the product view in my Post template? Thanks in advance for your help!

How do I change the 'name' HTML attribute of a Django Form field?

风格不统一 提交于 2021-02-11 17:35:42
问题 I have a Django 3.0 form # forms.py class SignupForm(UserCreationForm): email = forms.EmailField() This renders as the HTML element <input type="text" name="email" required id="id_email"> Is there a way to change the 'name' attribute? The widgets documentation suggests that either of these might work: # forms.py class SignupForm(UserCreationForm): email = forms.EmailField( widget = forms.TextInput( attrs = {'name': 'email_address'} ) ) or # forms.py class SignupForm(UserCreationForm): email =

Vue.js + Axios not assigning response

本小妞迷上赌 提交于 2021-02-11 17:34:52
问题 I have the following vue.js code using django as a backend. The response is coming through fine, but I can't seem to set the variable properly. Am I doing something wrong with the assignment? Thanks <script> import axios from 'axios' //Datatable export default { data () { return { items: [], } mounted () { this.getItems() }, methods: { getItems() { const newLocal='http://127.0.0.1:8000/items/' axios({ method: 'get', url: newLocal, auth: { username: 'login', password: 'pass' } }).then(response

Vue.js + Axios not assigning response

瘦欲@ 提交于 2021-02-11 17:34:25
问题 I have the following vue.js code using django as a backend. The response is coming through fine, but I can't seem to set the variable properly. Am I doing something wrong with the assignment? Thanks <script> import axios from 'axios' //Datatable export default { data () { return { items: [], } mounted () { this.getItems() }, methods: { getItems() { const newLocal='http://127.0.0.1:8000/items/' axios({ method: 'get', url: newLocal, auth: { username: 'login', password: 'pass' } }).then(response

Vue.js + Axios not assigning response

夙愿已清 提交于 2021-02-11 17:34:06
问题 I have the following vue.js code using django as a backend. The response is coming through fine, but I can't seem to set the variable properly. Am I doing something wrong with the assignment? Thanks <script> import axios from 'axios' //Datatable export default { data () { return { items: [], } mounted () { this.getItems() }, methods: { getItems() { const newLocal='http://127.0.0.1:8000/items/' axios({ method: 'get', url: newLocal, auth: { username: 'login', password: 'pass' } }).then(response

what is the correct way to override the save method in django?

扶醉桌前 提交于 2021-02-11 17:22:52
问题 I have an image model where I can upload images and I want to optimize them with pillow, I did that but there is three problems: the images doesn't get saved in the correct folder. django has a feature when there is two files with the same name django adds a random string to its name but now with two images with the same name only one gets uploaded. the original images gets uploaded too. class Images(models.Model): image1 = models.ImageField(upload_to='images/%Y/', validators=

what is the correct way to override the save method in django?

核能气质少年 提交于 2021-02-11 17:21:56
问题 I have an image model where I can upload images and I want to optimize them with pillow, I did that but there is three problems: the images doesn't get saved in the correct folder. django has a feature when there is two files with the same name django adds a random string to its name but now with two images with the same name only one gets uploaded. the original images gets uploaded too. class Images(models.Model): image1 = models.ImageField(upload_to='images/%Y/', validators=

Use Django password widget and change password form in user admin site

落爺英雄遲暮 提交于 2021-02-11 17:09:53
问题 I am extending the user model in Django and have create a PIN number field that is hashing into the database correctly. However, in the Django admin when viewing a user the pin number field is populated with the hash. Is it possible to use the Django password widget: so that the PIN number field is not populated: and therefore by extension use the template for the change password form to also change the pin? 回答1: I don't see why you couldn't use the form. Here's the Django User form: django

Use Django password widget and change password form in user admin site

☆樱花仙子☆ 提交于 2021-02-11 17:08:33
问题 I am extending the user model in Django and have create a PIN number field that is hashing into the database correctly. However, in the Django admin when viewing a user the pin number field is populated with the hash. Is it possible to use the Django password widget: so that the PIN number field is not populated: and therefore by extension use the template for the change password form to also change the pin? 回答1: I don't see why you couldn't use the form. Here's the Django User form: django

Trying to export data from database to excel in django

泪湿孤枕 提交于 2021-02-11 17:01:15
问题 views.py def export(request): print('start') ourid = request.POST.getlist("terid") queryset = Case_Info.objects.filter(id__in=list(map(int, ourid))) Case_Detail = Case_Info_Resource() print(ourid) dataset = Case_Detail.export(queryset) # error in this line response = HttpResponse( dataset.xls, content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="persons.xls"' print('end') return response Ajax Script $(document).ready(function () { $('#Download')