django-views

How to render data to a {% included a.html %} template in Django

你离开我真会死。 提交于 2019-11-28 12:12:57
问题 I have a rank.html which is a publicly sharing template for many other templates through {% include rank.html %} method. This template will display the 48 hours hot news base on the click number. Here is the view.py: def rank(self, request): hot_news_48h = h_mostViewed(48, News, '-pv') return render(request, "rank.html", { 'hot_news_48h': hot_news_48h,}) h_mostViewed(48, News, '-pv') is a function,that can fetch most viewed(clicked) post within 48 hours.It works. Here is the rank.html: <ul> {

Error while accessing request.session['key'] inside forms. [using CheckboxSelectMultiple]

自作多情 提交于 2019-11-28 11:55:56
问题 I have two forms named GoodAtForm and PaidForForm . What these do is as follows... GoodAtForm Takes an input from a list in request.session['love'] and presents it to the user. Then user is presented with a CheckboXSelectMultiple fields so that users can select. After The form is submitted in the view, the user choices are then stored inside another list request.session['good'] . 4.Another Form named PaidForForm uses that list for further asking of questions from users using

Excluding fields in generic CRUD views

被刻印的时光 ゝ 提交于 2019-11-28 10:57:09
问题 I have a model named Domain which looks like this: class Domain(models.Model): """ Model for storing the company domains """ user = models.ForeignKey( User ) host = models.CharField( null=False, verbose_name="Host", max_length=128, unique=True ) I'd like to use Django's generic views for doing CRUD operations on this. There is one field in this model that needs user input but the foreign key field doesn't need any user input. How can I exclude that field from the form that my generic view

showing the checked item alone without check box

孤街浪徒 提交于 2019-11-28 10:51:32
问题 forms.py PERSON_ACTIONS = ( ('1', '01.Allowed to rest and returned to class'), ('2', '02.Contacted parents /guardians'), ('3', '02a.- Unable to Contact'), ('4', '02b.Unavailable - left message'),) class PersonActionsForm(forms.ModelForm): action = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), choices=PERSON_ACTIONS, required=False, label= u"Actions") models.py class Actions(models.Model): report = models.ForeignKey(Report) action = models.IntegerField('Action type') print

How can create a model form in django with a one-to-one relation with another model

有些话、适合烂在心里 提交于 2019-11-28 10:49:22
问题 I want to create a model form with a one-to-one relation with another model. i.e Model1 has a one-to-one relation with Model2. I want my form to show all the fields from Model1 as well as Model2. Also what is the best way to show this in a view. 回答1: You don't need to create the single form for two models. Use two django forms and place them inside the single <form> tag: class Model1Form(forms.ModelForm): class Meta: model = Model1 class Model2Form(forms.ModelForm): class Meta: model = Model2

I can't update my post using modelformset factory

☆樱花仙子☆ 提交于 2019-11-28 10:30:02
问题 i am using two models named Posts and PostPicture and posts model have multiple text fields whereas PostPicture model have one image field and one posts foreignkey field and i am using formset_factory on image field in views to let the user upload multiple images with the post, the problem is that whenever i try to update the post it updates the text fields of Posts model but it doesn't update the images i upload with it...there's something wrong with my update view and i can't figure out

“Lazy load” of data from a context processor

六月ゝ 毕业季﹏ 提交于 2019-11-28 10:06:51
In each view of my application I need to have navigation menu prepared. So right now in every view I execute complicated query and store the menu in a dictionary which is passed to a template. In templates the variable in which I have the data is surrounded with "cache", so even though the queries are quite costly, it doesn't bother me. But I don't want to repeat myself in every view. I guessed that the best place to prepare the menu is in my own context processor. And so I did write one, but I noticed that even when I don't use the data from the context processor, the queries used to prepare

is there a way to loop over two lists simultaneously in django?

巧了我就是萌 提交于 2019-11-28 09:56:27
I have two list objects of the same length with complementary data i want to render is there a way to render both at the same time ie. {% for i,j in table, total %} {{ i }} {{ j }} {% endfor %} or something similar? If both lists are of the same length, you can return zipped_data = zip(table, total) as template context in your view, which produces a list of 2-valued tuples. Example: >>> lst1 = ['a', 'b', 'c'] >>> lst2 = [1, 2, 3] >>> zip(lst1, lst2) [('a', 1), ('b', 2), ('c', 3)] In your template, you can then write: {% for i, j in zipped_data %} {{ i }}, {{ j }} {% endfor %} Also, take a look

Django 2.1.3 Error: __init__() takes 1 positional argument but 2 were given

牧云@^-^@ 提交于 2019-11-28 08:44:36
问题 i am trying to develop a website whith Django 2.1.3 and python 3.7.1 When i go to the homepage i get this error: TypeError at / __init__() takes 1 positional argument but 2 were given Here some details about the code i write: Trceback Environment: Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 2.1.3 Python Version: 3.7.1 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib

Does Django automatically detect the end user's timezone?

萝らか妹 提交于 2019-11-28 08:15:18
问题 I am building an application in Django which allows the end-user to retrieve information which is sensitive to the time of day (12 am to 12 am) on a given day. I store this information in my database as an integer representing the seconds since midnight in 30-minute increments. I was looking at Django's timezone documentation: https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/ and found myself confused on whether or Django automatically uses the end-users time, or if I must collect