我通过看django教程创建了一个网站。 https://docs.djangoproject.com/en/1.11/intro/ 我收到了一个错误 / polls / 1 /的AttributeError '方法'对象没有属性' inner_html' 。 Traceback说
Traceback:
File "/Users/XXX/django/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/Users/XXX/django/django/core/handlers/base.py" in _get_response
130. response = self.process_exception_by_middleware(e, request)
File "/Users/XXX/django/django/core/handlers/base.py" in _get_response
128. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/XXX/djangostudy/polls/views.py" in detail
40. form = VoteForm(question=obj)
File "/Users/XXX/djangostudy/polls/forms.py" in __init__
21. self.fields['choice'].widget.render.inner_html = '{choice_value}{sub_widgets}<br>'
Exception Type: AttributeError at /polls/1/
Exception Value: 'method' object has no attribute 'inner_html'
我的forms.py就像
from django import forms
class MyForm(forms.Form):
text = forms.CharField(max_length=100,required=False,label='テキスト')
class VoteForm(forms.Form):
choice = forms.ModelChoiceField(
queryset=None,
label='選択',
widget=forms.RadioSelect,
empty_label=None,
error_messages={
'required':"You didn't select a choice.",
'invalid_choice':"invalid choice.",
},
)
def __init__(self,question,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['choice'].queryset = question.choice_set.all()
self.fields['choice'].widget.render.inner_html = '{choice_value}{sub_widgets}<br>'
单选按钮的设置写在detail.html中,如
<!DOCTYPE html>
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'poll_vote' question.id %}" method="post">
<!--<form action="" method="post">-->
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<!--{{ form }}-->
<input type="submit" value="Vote" />
</form>
</html>
views.py是
from django.shortcuts import render
from django.utils.html import mark_safe
from .models import Question
from django.http import HttpResponse
from django.shortcuts import Http404
from django.shortcuts import get_object_or_404,redirect
from .models import Choice
from django.views.generic import TemplateView
from django.views.generic import DetailView
from django.views.generic import ListView
from .forms import MyForm
from .forms import VoteForm
# Create your views here.
def index(request):
return render(request,'polls/index.html',{
'questions': Question.objects.all(),
})
def detail(request,pk):
obj = get_object_or_404(Question,pk=pk)
if request.method == "POST":
form = VoteForm(question=obj,data=request.POST)
if form.is_valid():
return redirect('polls:results',pk)
else:
form = VoteForm(question=obj)
return render(request,'templates/polls/detail.html',{
'form':form,
'question': obj,
})
def vote(request,pk):
pass
def results(request,pk):
obj = get_object_or_404(Question,pk=pk)
return render(request,'polls/results.html',{
'question':obj,
})
def form_test(request):
if request.method == "POST":
#request.POST???
form = MyForm(data=request.POST)
if form.is_valid():
pass
else:
form = MyForm()
return render(request,'polls/form.html',{
'form':form,
})
我不知道如何修复它。我无法理解为什么会发生这种错误。我应该怎么做才能解决这个问题?
教程有inner_html
,所以我认为有必要做一些必要的事情。但是,它没用吗?
我想要显示页面
在这一部分
self.fields [&#39; choice&#39;]。widget.render.inner_html =&#39; {choice_value} {sub_widgets}
&#39;
我想让这个页面成为单选按钮页面。