我感到非常愚蠢和困惑,长期处理这个问题。 它完全正常,显示调查正常,但它不存储(保存)提交的表单到我的数据库。我看过很多问题/解决方案,但是没有一个能帮助我。
以下是我的models.py
from __future__ import unicode_literals
from django.db import models
#from multiselectfield import MultiSelectField,
from django.contrib.auth.models import User
#from django.forms import ModelForm
# Create your models here.
class user_prefs(models.Model):
cuisine_choice = (
('1', 'Italian'),
('2', 'American'),
('3', 'Japanese'),
('4', 'French'),
('5', 'Mexican'),
('6', 'Chinese'),
('7', 'Indian'),
('8', 'Middle Eastern')
)
lunch_pref = (
('1', 'Coffeehouse'),
('2', 'Cafe'),
('3', 'Restaurant'),
('4', 'Fast Food'),
('5', 'Takeaway'),
('6', 'Stake House')
)
dinner_pref = (
('1', 'Restaurant'),
('2', 'Takeaway'),
('3', 'Delivery'),
('4', 'Fast food'),
('5', 'Coffeehouse'),
('6', 'Cafe'),
('7', 'Cooking at home')
)
sunday = (
('1', 'Cultural activities(Museums, Galleries, Exhibitions etc.)'),
('2', 'Sport activities'),
('3', 'Attending sport events'),
('4', 'Music events'),
('5', 'Hiking'),
('6', 'Going to park')
)
friday = (
('1', 'Bar'),
('2', 'Nightclub'),
('3', 'Karaoke'),
('4', 'Netflix & chill'),
('5', 'Videogames'),
('6', 'Cinema'),
('7', 'Theater'),
('8', 'Restaurant')
)
userID = models.ForeignKey(User) #related_name='User', null=True
Cuisine = models.IntegerField(choices=cuisine_choice)
Cuisine1 = models.CharField(max_length=30)
LunchPref = models.IntegerField(choices=lunch_pref)
DinnerPref = models.IntegerField(choices=dinner_pref)
Sunday = models.IntegerField(choices=sunday)
Friday = models.IntegerField(choices=friday)
到目前为止对我来说似乎很好。
这里是ModelForm(forms.py)
class user_prefsForm(ModelForm):
class Meta:
model = user_prefs
fields = ['Cuisine', 'Cuisine1', 'LunchPref', 'DinnerPref', 'Sunday', 'Friday']
form = user_prefsForm()
这里是views.py
def display1(request):
if not request.user.is_authenticated:
return redirect(settings.LOGIN_URL)
#form = user_prefsForm(request.POST)
def display(request):
if request.method == 'POST':
form = user_prefsForm(request.POST)
if form.is_valid():
form.userID = request.user
form.save()
return HttpResponseRedirect('/')
else:
form = user_prefsForm()
return render(request, 'display.html', {'form': form})
模板
Please state your preferences below
<form action='/' method="POST" >
{% csrf_token %}
<p>What is you favourite cuisine?</p>
{{ form.Cuisine }}
<p>If not listed above or you want to add more to that, coud you please tell us? </p>
{{ form.Cuisine1 }}
<p>What would be your lunch preference ?</p>
{{ form.LunchPref }}
<p>What would be your Dinner preference ?</p>
{{ form.DinnerPref }}
<p>What would you do on your Friday free time ?</p>
{{ form.Sunday }}
<p>What would you do on Sunday?</p>
{{ form.Friday }}
<button type="submit">Submit</button>
{% endif %}
</html>
以防万一urls.py(但我不认为会有任何问题):
urlpatterns = [
url(r'^$', index),
url(r'^letusknow/', display, name='display'),
url(r'^letusknow/', display1, name='display1'),
]
最初,我的模型中有模型.CharField,它也没有用。
答案 0 :(得分:1)
您的问题是,无论表单是否有效,您都将重定向到主页。
def display(request):
if request.method == 'POST':
form = user_prefsForm(request.POST)
if form.is_valid():
form.userID = request.user
form.save()
# note indentation change here.
return HttpResponseRedirect('/')
else:
form = user_prefsForm()
# note identation change here.
return render(request, 'display.html', {'form': form})
此外,在模板中,您无法正确渲染字段,表单中的某些错误将永远不会显示。请参阅此链接:https://docs.djangoproject.com/en/1.11/topics/forms/#rendering-fields-manually
除非您修复模板,否则用户永远不会知道他填写的表单不正确。