I am trying to make quiz making application, since am new to django am unable to build the logic for saving foreign key field in database table. Someone please help me for the same.
models.py
In models.py , class quiztitle is for title of the quiz and id(foreign key,User model) of user who created that quiz.
class question is for question along with 4 options and the correct answer. Quizid(foreign key,quiztitle model) and id(foreign key,User model)
class answer is for the answer submitted by user who take the quiz.
from django.db import models
from django.contrib.auth.models import User
class quiztitle(models.Model):
Quiz_id = models.AutoField(primary_key=True)
Quiz_title = models.CharField(max_length=600)
id= models.ForeignKey(User, on_delete=models.CASCADE)
class question(models.Model):
Qid = models.AutoField(primary_key=True)
id = models.ForeignKey(User,on_delete=models.CASCADE)
Quiz_id = models.ForeignKey(quiztitle,on_delete=models.CASCADE)
Qques = models.TextField()
Qoption1 = models.TextField()
Qoption2 = models.TextField()
Qoption3 = models.TextField()
Qoption4 = models.TextField()
QAnswer = models.TextField()
class answer(models.Model):
Ansid = models.AutoField(primary_key=True)
Qid = models.ForeignKey(question,on_delete=models.CASCADE)
Quiz_id = models.ForeignKey(quiztitle, on_delete=models.CASCADE)
id = models.ForeignKey(User, on_delete=models.CASCADE)
Answer = models.TextField()
forms.py
from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
class CreateUserForm(UserCreationForm):
class Meta:
model = User
fields = ['username','email','password1','password2']
views.py
from django.shortcuts import render,redirect,HttpResponseRedirect
from .models import question ,quiztitle
from django.contrib import messages
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.forms import inlineformset_factory
from django.contrib.auth.forms import UserCreationForm
from .forms import CreateUserForm
from django.contrib import messages
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import Group
# Create your views here.
#login_required(login_url='home')
def handle_quiz(request):
if request.method=="POST":
# get post parameters
id = request.POST.get('id')
Quiz_title = request.POST.get('Quiz_title')
Quiz_id = request.POST.get('Quiz_id')
Qid = request.POST.get('Qid')
Qques = request.POST.get('Qques')
Qoption1 = request.POST.get('Qoption1')
Qoption2 = request.POST.get('Qoption2')
Qoption3 = request.POST.get('Qoption3')
Qoption4 = request.POST.get('Qoption4')
QAnswer = request.POST.get('QAnswer')
#I guess here is the mistake in saving the data in the mysql database
title = quiztitle(Quiz_title=Quiz_title,Quiz_id=Quiz_id,id=id)
title.save()
detail = question(Qid=Qid,Quiz_id=Quiz_id,id=id,Qques=Qques,Qoption1=Qoption1,Qoption2=Qoption2,Qoption3=Qoption3,Qoption4=Qoption4,QAnswer=QAnswer)
detail.save()
messages.success(request,"Your question has been added succesfully ")
return HttpResponseRedirect('/quizmaker')
return render(request,"createquiz.html")
def logoutUser(request):
logout(request)
return redirect('home')#redirect to login page
def home_page(request):
return render(request,'Home.html')
def registerPage(request):
if request.user.is_authenticated:
return redirect('home')
else:
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
user = form.save()
username = form.cleaned_data.get('username')
messages.success(request, 'account has been created successfully for username' + username)
return redirect('login')
context = {'form':form}
return render(request,'register.html',context)
def handle_login(request):
if request.user.is_authenticated:
return redirect('home')
else:
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('quizmaker')
else:
messages.info(request, 'Incorrect Username or Password')
context = {}
return render(request, 'login.html', context)
#login_required(login_url='login')
def handle_response(request):
data= question.objects.all()
return render(request, "student.html", {"messages": data})
admin.py
from django.contrib import admin
from .models import *
# Register your models here.
class quizadmin(admin.ModelAdmin):
list_display = ['Qid','Quiz_id','Qques','Qoption1','Qoption2','Qoption3','Qoption4','QAnswer']
admin.site.register(question,quizadmin)
admin.site.register(quiztitle)
As you guess your issue lies at title and detail.
To set a value to the ForeignKey you need an object. You can use instead an field with _id at the end.
Behind the scenes, Django appends "_id" to the field name to
create its database column name, see Django ForeignKey
It must be:
title = quiztitle.objects.create(
Quiz_title=Quiz_title,
Quiz_id=Quiz_id,
User_id_id=id # here
)
detail = question.objects.create(
Quiz_id_id=Quiz_id, User_id_id=id # and here,
Qid=Qid, Qques=Qques, Qoption1=Qoption1,
Qoption2=Qoption2, Qoption3=Qoption3,
Qoption4=Qoption4, QAnswer=QAnswer
)
I suggest you to use Django ModelForm here.
Based on the above, I recommend you also to rename your ForeignKey fields:
id > to user
Quiz_id > to quiztitle
Model style (Django Docs):
Use InitialCaps for class names (or for factory functions that return
classes).
Field names should be all lowercase, using underscores instead of
camelCase.
Related
I am creating a donation app that allows donors to create listings. This data is stored in a Django Model and is going to be displayed on a page. I want to save the user's username to the Django model and display it on the page. My code is down below
Models.py
class Donation(models.Model):
title = models.CharField(max_length=30)
phonenumber = models.CharField(max_length=12)
category = models.CharField(max_length=20)
image = models.CharField(max_length=1000000)
deliveryorpickup = models.CharField(max_length=8)
description = models.TextField()
Views.py
from django.contrib.auth.models import User
from django.http.request import RAISE_ERROR
from django.http.response import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.forms import forms, inlineformset_factory
from django.contrib.auth.forms import UserCreationForm, UsernameField
from .forms import CreateUserForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from home.models import Donation
# Create your views here.
def index(request,*args, **kwargs):
return render(request, "index.html", {} )
#login_required(login_url='/login/')
def dashboard(request,*args, **kwargs):
return render(request, "dashboard.html", {} )
def register(request, ):
if request.user.is_authenticated:
return redirect('/dashboard/')
else:
form = CreateUserForm()
if request.method == "POST":
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, f'Your account has been successfully created, {username} ')
return redirect('loginpage')
context = {'form': form}
return render(request, "register.html", context )
def loginpage(request):
if request.user.is_authenticated:
return redirect('/dashboard/')
else:
if request.method == 'POST':
username = request.POST.get('username')
password =request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/dashboard')
else:
messages.error(request, 'Username OR password is incorrect')
context = {}
return render(request, 'login.html', context)
def logoutuser(request):
logout(request)
return HttpResponseRedirect('/login/')
#login_required(login_url='/login/')
def donate(request):
if request.method == "POST":
title = request.POST['donationtitle']
phonenumber = request.POST['phonenumber']
category = request.POST['category']
image = request.POST['imagelink']
deliveryorpickup = request.POST['deliveryorpickup']
description = request.POST['description']
ins = Donation(title = title, phonenumber = phonenumber, category = category, image = image, deliveryorpickup = deliveryorpickup, description = description )
ins.save()
return render(request,'donate.html')
Forms.py (This is where the user is created)
class CreateUserForm(UserCreationForm):
username = forms.CharField(required=True, max_length=30, )
email = forms.EmailField(required=True)
first_name = forms.CharField(required=True, max_length=50)
last_name = forms.CharField(required=True, max_length=50)
class Meta:
model = User
fields = ['username', 'email', 'first_name', 'last_name', 'password1', 'password2',]
#function to display errors
def clean(self):
cleaned_data=super().clean()
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if User.objects.filter(username=cleaned_data["username"]).exists():
raise ValidationError("This username is taken, please try another one")
elif password1 != password2:
raise forms.ValidationError("2 password fields do not match")
elif len(password1) < 8 or len(password2) < 8:
raise forms.ValidationError("Passwords must be at least 8 characters long")
To associate the user with the Donation model, you should first add a ForeignKey field to the model class:
from django.conf import settings
class Donation(models.Model):
... # your other donation fields
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
blank=True,
null=True,
)
Once you've made this change, and run the migrations, in your views.py you'll pass the currently signed in user to the Donation model creation:
#login_required(login_url='/login/')
def donate(request):
if request.method == "POST":
ins = Donation(
title=request.POST["title"],
... # all of the other fields
user=request.user, # 👈 This adds the user
)
ins.save()
return render(request,'donate.html')
Notes
Using settings.AUTH_USER_MODEL allows your class to use a custom user model, or django's default user model, based on your project's settings.
To understand what on_delete=models.CASCADE does, you should read django's documentation about it.
Also, instead of manually passing all of the request.POST[...] values to the Donation model, I recommend that you use a ModelForm. It will handle errors and validation for you, as well as generate the HTML displayed in the template. Using a model form here would make your view code change to this:
from django.forms import ModelForm
class DonationForm(ModelForm):
class Meta:
model = Donation
exclude = ["user"]
#login_required(login_url="/login/")
def donate(request):
if request.method == "POST":
form = DonationForm(request.POST)
if form.is_valid():
donation = form.save(commit=False)
donation.user = request.user
donation.save()
# Use a redirect to prevent duplicate submissions
# https://docs.djangoproject.com/en/3.2/topics/http/shortcuts/#redirect
return redirect(request, ...)
else:
form = DonationForm()
return render(request, "donate.html", {"form": form})
I have a form to let users make a new poll, it consists of 2 ModelForms one for the Question model and another for the Choice model
a screenshot:
Make new poll form
i want there to be something like a (+) or (add another choice) button where users can add as many choices as they want and then submit it all.
here's the models.py file
models.py
from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()
class Question(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
questionText = models.CharField(max_length=150)
datePublished = models.DateTimeField(auto_now_add=True)
def str(self):
return self.questionText
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choiceText = models.CharField(max_length=200)
choiceVotes = models.IntegerField(default=0)
def str(self):
return self.choiceText
forms.py
from django import forms
from .models import Question
from .models import Choice
class NewPollForm(forms.ModelForm):
class Meta:
model = Question
labels = {
"questionText":"Poll question"
}
fields = ("questionText",)
class NewChoicesForm(forms.ModelForm):
class Meta:
model = Choice
labels = {
"choiceText":"Choice"
}
fields = ("choiceText",)
the view for this form's page you can see it is set for only one choice field right now
from .forms import NewPollForm
from .forms import NewChoicesForm
def newpoll(request):
if request.user.is_authenticated == True :
pass
else:
return redirect("users:signup")
if request.method == "POST":
qform = NewPollForm(request.POST)
cform = NewChoicesForm(request.POST)
if qform.is_valid():
newpoll = qform.save(commit=False)
newpoll.user = request.user
newpoll.datePublished = timezone.now()
newpoll.save()
cform.instance.question = newpoll
cform.save()
return redirect("polls:details", pollid=newpoll.pk)
else:
return render (request, "polls/newpoll.html", {"qform":qform, "cform":cform})
else:
qform = NewPollForm()
cform = NewChoicesForm()
return render (request, "polls/newpoll.html", {"qform":qform, "cform":cform})
I am working on a Todo list which has seperate users with their own list.
Initially I had the models.py as:
from django.db import models
from django.contrib.auth.models import User
class Todo(models.Model):
text = models.CharField(max_length=40, default='Test User')
complete = models.BooleanField(default = False)
task_priority = models.CharField(max_length=40, default='high')
def __str__(self):
return self.text
But then when im trying to link single user with its own set of todotask(text field) and its priorites and status.
I added one to one field like this:
from django.db import models
from django.contrib.auth.models import User
class Todo(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key='user_id') #new
text = models.CharField(max_length=40, default='Test User')
complete = models.BooleanField(default = False)
task_priority = models.CharField(max_length=40, default='high')
def __str__(self):
return self.text
It threw an error like this:
FieldError at /
Cannot resolve keyword 'id' into field. Choices are: complete, task_priority, text, user, user_id
I didnt completely understand the concept of one to one fields and looking at the tutorials online im confused weather I need to make another table and link it to the Todo table or add another field with some primary key?
Help me understand this concept.
Thanks in advance.
EDIT:
views.py file:
from django.shortcuts import render, redirect
from .models import Todo
from .form import TodoForm,ContactForm
import datetime
from django.conf import settings
from django.utils import timezone
from django.views.decorators.http import require_POST
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
import json
def index(request):
todo_list = Todo.objects.order_by('id')
form = TodoForm()
mydate = datetime.datetime.now()
context = {'todo_list': todo_list, 'form': form, 'mydate':mydate}
return render(request,'todo/index.html',context)
def login(request):
return render(request, 'todo/login.html')
#require_POST
def addTodo(request):
form = TodoForm(request.POST)
text_input = form.get_cleaned_data['text']
priority_input = form.get_cleaned_data['task_priority']
if form.is_valid():
new_todo = Todo(text = text_input, task_priority = priority_input)
# new_todo = Todo(text = request.POST['text'])
new_todo.save()
return redirect('index')
def completeTodo(request, todo_id):
todo = Todo.objects.get(pk=todo_id)
todo.complete = True
todo.save()
return redirect('index')
def deleteCompleted(request):
Todo.objects.filter(complete__exact=True).delete()
return redirect('index')
def deleteAll(request):
Todo.objects.all().delete()
return redirect('index')
def emailView(request):
todo = Todo.objects.all();
task_list = []
status_list = []
for item in todo:
stritem=str(item)
task_list.append(stritem)
if item.complete == True:
status_list.append('complete')
else:
status_list.append('incomplete')
if request.method == 'GET':
form = ContactForm()
else:
form = ContactForm(request.POST)
if form.is_valid():
from_email = form.cleaned_data['from_email']
message = form.cleaned_data['message']
#maillist = Todo.objects.all()
email_list=dict(zip(task_list,status_list))
strlist = json.dumps(email_list)
content = message + strlist
try:
send_mail('Todo List', content ,settings.EMAIL_HOST_USER,[from_email])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('success')
return render(request, "email.html", {'form': form})
def successView(request):
return HttpResponse('Success! Thank you for your message.')
New models.py:
from django.db import models
from django.contrib.auth.models import User
class Todo(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
text = models.CharField(max_length=40, default="")
complete = models.BooleanField(default = False)
task_priority = models.CharField(max_length=40, default='high')
def __str__(self):
return self.text
Encountered
IntegrityError at /add NOT NULL constraint failed: todo_todo.user_id
This:
user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key='user_id')
isn't at all doing what you think it is. primary_key expects a boolean, not a field name, and isn't to do with the relationship at all. Because the string 'user_id' is not empty, this is being interpreted as True, and is equivalent to:
user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
which means that user is now the primary key for the Todo model. This is not what you want.
Remove that primary_key clause.
I am using python and django to develop an web application where I have built a CustomUser model extending the user model and also built a sign up form. But problem is that each time I am registering a new user when I go back to login page and enter the user name and password, it keeps giving the message "Username and Password does not match".
I am pasting the codes here:
forms.py
import re
from django.utils.translation import ugettext_lazy as _
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from models import CustomUser
import pdb
class RegistrationForm(UserCreationForm):
"""A form for creating new users. Includes all the required
fields, plus a repeated password."""
first_name = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("First name"), error_messages={ 'invalid': _("This value must contain only letters") })
last_name = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Last name"), error_messages={ 'invalid': _("This value must contain only letters") })
password1 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password (again)"))
date_of_birth = forms.DateField(widget=forms.TextInput(attrs= {'class':'datepicker'}))
sex = forms.ChoiceField(choices=(('M', 'MALE'), ('F', 'FEMALE')), label=_("Sex"))
voter_id = forms.CharField(widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Voter Id"))
is_election_staff = forms.BooleanField(initial=False, required=False)
class Meta:
model = CustomUser
fields = ['first_name', 'last_name', 'voter_id', 'date_of_birth', 'sex', 'is_election_staff']
def clean_username(self):
try:
user = User.objects.get(voter_id__iexact=self.cleaned_data['voter_id'])
except User.DoesNotExist:
return self.cleaned_data['voter_id']
raise forms.ValidationError(_("The user with given voter id already exists. Please try another one."))
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_("The two password fields did not match."))
return self.cleaned_data
def save(self, commit=True):
# Save the provided password in hashed format
user = super(RegistrationForm, self).save(commit=False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.date_of_birth = self.cleaned_data['date_of_birth']
user.sex = self.cleaned_data['sex']
user.voter_id = self.cleaned_data['voter_id']
user.is_election_staff = self.cleaned_data['is_election_staff']
user.username = user.voter_id
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user
models.py
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser)
class Party(models.Model):
name = models.CharField(max_length=200)
num_seats_won = models.IntegerField(default=0)
SEX = (('M', 'MALE'), ('F', 'FEMALE'))
class CustomUser(AbstractBaseUser):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
date_of_birth = models.DateField()
sex = models.CharField(choices=SEX, max_length=10)
is_election_staff = models.BooleanField(default=True)
voter_id = models.CharField(max_length=40, unique=True)
USERNAME_FIELD = 'voter_id'
def __str__(self):
return "%s %s" % (self.first_name, self.last_name)
voting/urls.py: (Here voting is my application name)
urlpatterns = [
# url(r'^$', views.index, name='index'),
url(r'^$', 'django.contrib.auth.views.login'),
url(r'^logout/$', logout_page),
url(r'^register/$', register),
url(r'^register/success/$', register_success),
url(r'^home/$', home),
]
views.py
from .forms import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.views.decorators.csrf import csrf_protect
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
import pdb
#csrf_protect
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
print "In register request = "+ str(request.POST)
form.save()
return HttpResponseRedirect('/voting/register/success/')
else:
form = RegistrationForm()
variables = RequestContext(request, {
'form': form
})
return render_to_response(
'registration/register.html',
variables,
)
def register_success(request):
print "In success request = "+ str(request)
return render_to_response(
'registration/success.html',
)
def logout_page(request):
logout(request)
return HttpResponseRedirect('/voting/')
#login_required
def home(request):
return render_to_response(
'home.html',
{ 'user': request.user }
)
Can anyone please suggest me the mistake that I am doing here? Thanks in advance.
Your Override the user model but have make change in backend.py which is use to check when user try to login in this file i think django checking username and password from user model.
I´m working with Django Framework and it throws this exception: ModelForm has no model class specified.
This is my code:
views.py
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import redirect
from DjangoApp1.forms import login_form
from django.shortcuts import render #For render templates
def login_view(request):
form = login_form()
context = { 'form': form, 'mensaje':'Logeandose'}
if request.method == 'POST':
form = login_form(request.POST)
usuario = request.POST.get('username')
contrase = request.POST.get('password')
# Hacer el login
user = authenticate(username=usuario, password=contrase)
if user is not None and user.is_active:
login(request, user)
context['mensaje'] = u'Logeado como %s, contraseña %s' % (usuario, contrase)
else:
context['mensaje'] = u'No usuario o contraseña incorrecta'
return render (request, 'DjangoApp1/login.html', context)
And the models.py where I´ve the login form:
models.py
from django.contrib.auth.models import User
from django import forms
class login_form(forms.ModelForm):
username = forms.SlugField (max_length=8,
label='Usuario: ')
password = forms.SlugField (max_length=8,
widget=forms.PasswordInput(),
label='Contraseña:',
help_text='Hasta 8 letras')
class Meta:
model = User
fields = ('username', 'password')
You need to indent your class Meta because it's part of the model form class definition:
class login_form(forms.ModelForm):
username = forms.SlugField (max_length=8,
label='Usuario: ')
password = forms.SlugField (max_length=8,
widget=forms.PasswordInput(),
label='Contraseña:',
help_text='Hasta 8 letras')
class Meta:
model = User
fields = ('username', 'password')
Django doc explains this in details.