首先,我想为我的英语道歉。我用英语写这个问题很难,但我没有在俄罗斯网站上找到任何关于我的问题的想法。所以,我想在学校网站上创建一个时间表。在我的 models.py :
中class Classes(models.Model):
class Meta:
db_table = 'classes'
verbose_name_plural = "School_classes"
verbose_name = "School_class"
name = models.CharField(max_length=50, unique=True, verbose_name = 'School_classes')
def __str__(self):
return self.name
class Lessons(models.Model):
class Meta():
db_table = "lessons"
verbose_name_plural = "Lessons"
verbose_name = "Lessons"
lessons_class = models.ForeignKey(Classes)
lessons_creationdate = models.DateField()
lessons_weekcontent = RichTextField()
然后,在 forms.py 中,我有:
class LessonsForm(forms.ModelForm):
classname = forms.ModelChoiceField(queryset = Classes.objects.all(),empty_label="Choose a class",widget=forms.Select(attrs={'class':'dropdown'}),label="School_classes")
class Meta:
model = Lessons
fields = ('lessons_class',)
在我的 views.py :
中def ShowTimetable(request):
LForm = LessonsForm(request.POST or None)
args = {}
args['classes'] = Classes.objects.all()
args['lessons'] = Lessons.objects.all()
args['showlessons'] = LForm
args.update(csrf(request))
if request.method == 'POST' and LForm.is_valid():
CurrentClass = LForm.cleaned_data('currentclass', None)
args['class_name'] = CurrentClass
args['lessons'] = Lessons.objects.filter(lessons_class = CurrentClass)
if args['lessons']:
return render_to_response('Weekcontentpage.html',args )
else:
return render_to_response('Weekcontentpage_null.html',args )
else:
return render_to_response('myview.html',args )
最后,在myviews.html
{% extends 'base.html' %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ showlessons }}
<input type="submit" value="Show timetable">
</form>
{% endblock %}
在Weekcontentpage.html中:
{% extends "base.html" %}
{% block content %}
<h4>On request "{{ class_name }}:</h4>
{% for lesson in lessons %}
<h6>Date:{{ lesson.lessons_creationdate }}</h6>
<h4>{{ lesson.lessons_weekcontent }}</h4>
{% endfor %}
{% endblock content %}
,在Weekcontentpage_null.html中: {%extends“base.html”%}
{% block content %}
<h4> On "{{class_name}}" is nothing found :( (</h4>
{% endblock content %}
现在,在我在下拉菜单中选择并按下“显示时间表”按钮后,我只看到“无”并且没有找到任何内容:(。我明白了views.py中的问题,所以我无法从下拉中获取价值,但我不知道如何解决它。所以我将不胜感激任何帮助。谢谢和抱歉我的英语:)
答案 0 :(得分:1)
与任何其他领域相同。
问题是你已经定义了一个表单类,但你根本没有以任何方式使用它。您应该使用POST数据实例化表单,在其上调用request.POST
,然后从is_valid()
获取结果,而不是忽略表单并从form.cleaned_data
获取数据。