Reputation: 53
I feel very stupid and confused, tackling this issue for so long. It works completely fine, displays survey fine, however it does not store(save) submited form to my database. I have seen tons of questions/solutions, however none of them helped me.
Here are my 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)
Seems fine to me so far.
here is ModelForm(forms.py)
class user_prefsForm(ModelForm):
class Meta:
model = user_prefs
fields = ['Cuisine', 'Cuisine1', 'LunchPref', 'DinnerPref', 'Sunday', 'Friday']
form = user_prefsForm()
here is 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})
template
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>
Just in case urls.py(tho i dont think there would be any issues, however):
urlpatterns = [
url(r'^$', index),
url(r'^letusknow/', display, name='display'),
url(r'^letusknow/', display1, name='display1'),
]
Initially I had models.CharField in my models, that didnt work either.
Upvotes: 1
Views: 63
Reputation: 53774
Your problem is that you are redirecting to the home page regardless of whether the form is valid or not.
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})
Additionally in your template you are not rendering the fields correctly, some of the errors in the form will never be displayed. Please refer to this link: https://docs.djangoproject.com/en/1.11/topics/forms/#rendering-fields-manually
Unless you fix the template the user will never know that he filled the form incorrectly.
Upvotes: 1