所以我正在构建django评估表单,向用户显示4个选项(同意,高度同意,不同意,中立),我做了模型和表格,但我不知道如何构建视图!
我尝试创建视图,但是我认为我无法以正确的方式保存表单。
我也确实创建了html页面,视图和html都可以使用,但是当我完成并按下“提交”按钮时,它会抛出此错误
"Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/evaluation/form/
Django Version: 2.2.3
Python Version: 3.7.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'cal',
'crispy_forms',
'phone_field',
'django_countries',
'rest_framework']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Development\LMS\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Development\LMS\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Development\LMS\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Development\LMS\django-calendar\cal\views.py" in evaluation_view
186. evaluation.save()
File "C:\Development\LMS\lib\site-packages\django\db\models\base.py" in save
741. force_update=force_update, update_fields=update_fields)
File "C:\Development\LMS\lib\site-packages\django\db\models\base.py" in save_base
779. force_update, using, update_fields,
File "C:\Development\LMS\lib\site-packages\django\db\models\base.py" in _save_table
870. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Development\LMS\lib\site-packages\django\db\models\base.py" in _do_insert
908. using=using, raw=raw)
File "C:\Development\LMS\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Development\LMS\lib\site-packages\django\db\models\query.py" in _insert
1186. return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Development\LMS\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
1334. for sql, params in self.as_sql():
File "C:\Development\LMS\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
1278. for obj in self.query.objs
File "C:\Development\LMS\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
1278. for obj in self.query.objs
File "C:\Development\LMS\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
1277. [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Development\LMS\lib\site-packages\django\db\models\sql\compiler.py" in prepare_value
1208. if value.contains_column_references:
Exception Type: AttributeError at /evaluation/form/
Exception Value: 'Query' object has no attribute 'contains_column_references'
"
views.py
{def evaluation_view(request):
form = EvaluationForm()
question = Evaluation.objects.all()
if request.method == "POST":
form = EvaluationForm(request.POST)
if form.is_valid():
evaluation = form.save(commit=False)
evaluation.question = question
evaluation.save()
return redirect('cal:calendar')
context = {
"form":form,
"question": question,
}
return render(request, 'cal/evaluation.html', context)}
models.py
{class Evaluation(models.Model):
MY_EVALUATION = (('higly agree', 'higly agree'),('agree', 'agree'),('disagree', 'disagree'),('neutral', 'neutral'))
option = models.CharField(max_length=120, choices=MY_EVALUATION)
question = models.TextField(max_length=250)
def __str__(self):
return self.question
}
forms.py
{class EvaluationForm(forms.ModelForm):
class Meta:
model = Evaluation
fields = ['id', 'option']}
evaluation.html
{{% extends "cal/base.html" %}
{% load crispy_forms_tags %}
{% block title %}Evaluation{% endblock title %}
{% block content %}
<br>
<div class="card">
<h5 class="card-header" >تقييم الدورة</h5>
<div class="card-body">
<p class="card-text">
{% for questions in question %}
<ol><li>{{questions.question}}</li></ol>
<form method="POST" action="{% url 'cal:evaluation-form' %}">
{% csrf_token %}
{{form|crispy}}
{% endfor %}
<input type="submit" value="submit" class="btn btn-outline-success">
</form>
</p>
</div>
</div>
{% endblock content %}}