Django FormModel fields not found - django

Currently, the template is only generating the Submit button without any input fields.
Also, if I change fields = "all" to fields= ["email","name"] it tells me that these fields do not exist.
Model:
class NewsletterSubscriber(models.Model):
email = EmailField(required=True, label='Email')
name = CharField(required=True, label='Name')
Serializer:
class SubscribeForm(forms.ModelForm):
class Meta:
model = NewsletterSubscriber
fields = "__all__"
View:
def subscribe(request):
form = SubscribeForm()
if request.method == 'POST':
form = SubscribeForm(request.POST)
if form.is_valid():
form.save()
# redirect to a success page
return render(request, 'subscribe.html', {'subscribeForm': form})
Template:
<form action="{% url 'subscribe' %}" method="post">
{% csrf_token %}
{{ subscribeForm.as_p }}
<input type="submit" value="Subscribe">
</form>

Give this a try:
class NewsletterSubscriber(models.Model):
email = EmailField(required=True)
name = CharField(required=True)
class SubscribeForm(forms.ModelForm):
class Meta:
model = NewsletterSubscriber
fields = ['email', 'name']
labels = {'email': 'Email', 'name': 'Name'}
Instead of fields you could instead also go for exclude which would then obviously list the fields that you do not want to show up.
All these attributes you can put in the Meta class: Modelformfactory

Solved.
Wrong:
class NewsletterSubscriber(models.Model):
email = EmailField(required=True, label='Email')
name = CharField(required=True, label='Name')
Correct:
class NewsletterSubscriber(models.Model):
email = models.EmailField(required=True, label='Email')
name = models.CharField(required=True, label='Name')

Related

Edit - __init__() got an unexpected keyword argument 'instance'

I am trying to create "Edit" for my form.
urls.py
url(r'^app_1/(?P<id>[-\w]+)/edit/$',views.edit, name = 'edit'),
forms.py
class ClanakForma(forms.ModelForm):
class Meta:
model = Clanak
fields = '__all__'
models.py
class Clanak(models.Model):
naslov = models.CharField(null=False, blank=True, max_length=120)
datumObjave = models.DateField(null=False, blank=False)
autor = models.CharField(null=False, blank=True, max_length=50)
email = models.EmailField(max_length=75, null=True, blank=True)
def __str__(self):
return str(self.naslov) + ', ' + str(self.datumObjave) + ', ' + str(self.autor)
views.py
def edit(request, id):
data = get_object_or_404(Clanak, id = id)
if request.method == "POST":
form = ClanakForma(request.Clanak, instance=data)
if form.is_vaild():
data = form.save(commit=False)
data.naslov = request.user
data.datumObjave = request.user
data.autor = request.user
data.email = request.user
return redirect('readAllNew')
else:
form = ClanakForma(instance=data)
template = 'readAllNew.html'
context = {'form': form}
return render(request, template, context)
readAllNew.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table border="1">
<tr>
<th>Naslov</th>
<th>Datum</th>
<th>Autor</th>
<th>Mail</th>
</tr>
{% for x in data %}
<tr>
<td>{{x.naslov}}</td>
<td>{{x.datumObjave}}</td>
<td>{{x.autor}}</td>
<td>{{x.email}}</td>
<td>delete</td>
<td>edit</td>
</tr>
{% endfor %}
</table>
</body>
</html>
So when I hit edit link at my "readAllNew.html" I get error:
TypeError at /app_1/5/edit/
init() got an unexpected keyword argument 'instance'
I think something is wrong with my view but I am not sure what.
-------------------------------UPDATE--------------------------------
As suggested I edited "forms.py" but now I am not seeing any form field so I can't edit nothing:
See screenshot
---------------- UPDATE 2 -------------------------
I created new html file as "edit.html"
I edited "views.py":
def edit(request, id):
data = get_object_or_404(Clanak, id = id)
if request.method == "POST":
form = ClanakForma(instance=data)
if form.is_vaild():
data = form.save(commit=False)
data.naslov = request.user
data.datumObjave = request.user
data.autor = request.user
data.email = request.user
return redirect('readAllNew.html')
else:
form = ClanakForma(instance=data)
template = 'edit.html'
context = {'form': form}
return render(request, template, context)
Now I see fields and informations in it properly, but when I change something and click on "SUBMIT" I get error:
AttributeError at /app_1/6/edit/
'ClanakForma' object has no attribute 'is_vaild'
Use forms.ModelForm instead of forms.Form
class Forma(forms.ModelForm):
naslov = forms.CharField(label='naslov')
datumObjave = forms.DateField(label='datumObjave')
autor = forms.CharField(label='autor')
email = forms.EmailField(label='email')
class Meta:
model = Clanak
fields = ('naslov', 'datumObjave', 'autor', 'email')
If you want to use all the fields of Model class in your form, you could specify the fields attribute of Meta class as fields = '__all__'
#example
class Forma(forms.ModelForm):
class Meta:
model = Clanak
fields = '__all__'
If you are using forms.ModelForm, you could avoid most of the implementation which makes things easier :)
Update
You need to render the form in your template.So, change your template as
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
You can't pass instance to simple form - it has to be ModelForm. Use this:
class Forma(forms.ModelForm):
class Meta:
model = Clanak
fields = ['naslov', 'datumObjave', 'autor', 'email']
labels = {
'naslov': 'naslov',
'datumObjave': 'datumObjave',
'autor': 'autor',
'email': 'email'
}

Is this approach correct to create forms using multiple models?

I had to create a form from which some details go to default.auth.user model and some to my custom model so after searching from various sources I did this:
Django Version :1.7
model.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
title_id = models.ForeignKey('Title')
mobile_number = models.CharField(max_length=10)
alternate_number = models.CharField(max_length=10)
date_of_birth = models.DateField()
profession_id = models.ForeignKey('Profession', null=True, blank=True)
house_no = models.CharField(max_length=100, blank=True, default='NA')
city_id = models.ForeignKey('City', null=True)
country_id = models.ForeignKey('Country', null=True)
state_id = models.ForeignKey('State', null=True)
locality_id = models.ForeignKey('Locality', null=True)
profile_picture_path = models.CharField(max_length=100, blank=True, default='NA')
forms.py:
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput(attrs={'id': 'password'}))
email = forms.CharField(widget=forms.TextInput(attrs={'id': 'email_id'}))
username = forms.CharField(widget=forms.TextInput(attrs={'id': 'username'}))
first_name = forms.CharField(widget=forms.TextInput(attrs={'id': 'first_name'}))
last_name = forms.CharField(widget=forms.TextInput(attrs={'id': 'last_name'}))
class Meta:
model = User
fields = ('email', 'username', 'first_name', 'last_name', 'password')
class ExtraDetailsForm(UserForm):
confirm_password = forms.CharField(widget=forms.PasswordInput(attrs=
{'id':'confirm_password'}),max_length=32,
required=True,)
class Meta:
model = UserProfile
fields = ('email', 'username', 'title_id', 'first_name', 'last_name',
'password', 'confirm_password',
'date_of_birth', 'mobile_number', )
My view.py is :
def register(request):
# A boolean vakue for telling whether the registration was successful
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
additional_details_form = ExtraDetailsForm(data=request.POST)
if user_form.is_valid() and additional_details_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
additional_details = additional_details_form.save(commit=False)
additional_details.user = user
additional_details.save()
registered = True
else:
print(user_form.errors, additional_details_form.errors)
else:
user_form = UserForm
additional_details_form = ExtraDetailsForm
return render(request,
'users/register.html',
{'user_form' : user_form, 'additional_details_form': additional_details_form, 'registerered': registered})
regsiter.html:
{% if registerered %}
<p>Thank you for register. check ur email , entered email was</p>
{% else %}
<form action="/users/register/" method="post">{% csrf_token %}
{{ additional_details_form.as_p }}
<input type="submit" value="Register" />
</form>
{% endif %}
Now the good thing is that everything is working fine and details are being stored as they should be.
But the bad thing is I do not know whether it is a correct approach or not as I did not find any tutorial/resource where this approach is used?
This is correct approach and you do it almost right. Just couple notes:
if user_form.is_valid() and additional_details_form.is_valid():
In this line if user_form is invalid then validation for additional_details_form will not run. To always validate both change it to:
if all([user_form.is_valid(), additional_details_form.is_valid()]):
In else statement you set form class to *_form variables. It should be form instances instead:
user_form = UserForm()
additional_details_form = ExtraDetailsForm()
And it may be a good idea to wrap your save code into one transaction :-)
I would recommend that you use just one form here that contains all fields.
There is no benefit to using two forms, especially since one inherits the other, this is odd behaviour when you are then passing the POST data into each of them.
Consolidate the fields into a single form and then override the 'clean' method of the form to be able to check that the two password fields match.
You can create a single form to save data into one or many different models and this is especially useful in your case since you need to validate the data for these different models together.
Ok, firstly ExtraDetailsForm shouldn't inherit from UserForm because they are for different models. It should look something like this instead:
class UserForm(forms.ModelForm):
confirm_password = forms.CharField(widget=forms.PasswordInput(attrs=
{'id':'confirm_password'}),max_length=32,
required=True,)
class Meta:
model = User
fields = ('email', 'username', 'first_name', 'last_name', 'password',
'confirm_password')
class ExtraDetailsForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('title_id', 'date_of_birth', 'mobile_number')
Then in your view:
from django.contrib.auth import login
from django.shortcuts import redirect, render
def register(request):
user_form = UserForm(data=request.POST or None)
profile_form = ExtraDetailsForm(data=request.POST or None)
if all([user_form.is_valid(), profile_form.is_valid()]):
user = user_form.save(commit=False)
user.set_password(user.password)
user.save()
profile = profile_form.save()
# probably at this point you want to login the new user:
login(request, user)
# it's good practice to do a redirect here, after a successful
# form post, eg to display success page, as this will
# prevent accidental re-posting data if user reloads the page
return redirect('registration_success')
else:
print(user_form.errors, profile_form.errors)
return render(
request,
'users/register.html',
{
'user_form' : user_form,
'profile_form' : profile_form,
}
)
def registration_success(request):
return render('registration_success.html')
Finally you need to output both forms in the template:
<form action="/users/register/" method="post">{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<input type="submit" value="Register" />
</form>
and a new template registration_success.html:
<p>Thank you for registering. Check your email, entered email was: {{ request.user.email }}</p>

Django Model form is not shown

I am probably missing something very simple, because my model form is not shown at the template. The code is very simple:
models.py:
class Story(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=200)
content = models.TextField()
picture = models.ImageField(upload_to = 'images/post_images')
date = models.DateTimeField(auto_now_add=True)
forms.py:
class StoryForm(forms.ModelForm):
class Meta:
model = Story
views.py:
from sfv.forms import StoryForm
#login_required(redirect_field_name=None)
def restricted(request):
user = request.user
form = StoryForm()
#graph = get_persistent_graph(request)
return render(request, "restricted.html", {user : 'user', form : 'form',})
template:
<form method = 'POST' action = ''>
<table>
{{ form }}
<table>
</form>
I have also tried form.as_p, that didnt help.
Change your render method from
return render(request, "restricted.html", {user : 'user', form : 'form',})
to
return render(request, "restricted.html", { 'user' : user, 'form' : form})
The key and value in the context dictionary were interchanged. Hence the issue.

Storing form data in the database

I can't figure out how to store a simple form in the database. I think I'm quite close but there is probably something wrong in my views.py. Here is my code, any ideas what I'm doing wrong? (also on dpaste)
# models.py
class IngredienceCategory(models.Model):
name = models.CharField(max_length=30, unique=True)
user = models.ForeignKey(User, null=True, blank=True)
class Meta:
verbose_name_plural = "Ingredience Categories"
def __unicode__(self):
return self.name
# forms.py
class CategoryForm(forms.Form):
name = forms.CharField(max_length=30)
# views.py
#login_required
def newCategory(request):
if request.method == 'POST':
username = request.user.username
cform = CategoryForm(request.POST)
if cform.is_valid():
formInstance = cform.save(commit = False)
formInstance.user = username
formInstance.name = cform.cleaned_data['name']
formInstance = IngredienceCategory.objects.filter(name=formInstance.name, user=formInstance.user)
formInstance.save()
# return HttpResponseRedirect('new-category/')
else:
form = CategoryForm()
context = {'form': form}
return render_to_response('new-category.html', context, context_instance=RequestContext(request))
# new-category.html
<h3>Insert New Category</h3>
<form action="/" method="post" id="food-form">{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="foodForm" value="Save" />
</form>
The line below is not useful at it current position. That command will perform a database query and assign the result as a queryset, before you have saved the form data.
formInstance = IngredienceCategory.objects.filter(name=formInstance.name, user=formInstance.user)
This should work:
With cform as a normal Form:
if cform.is_valid():
formInstance = IngredienceCategory(user=request.user, cform.cleaned_data['name'])
formInstance.save()
If cform had been a ModelForm you could do:
if cform.is_valid():
formInstance = cform.save(commit=False)
formInstance.user = request.user
formInstance.save()
I do recommend you to check out ModelForms since it will build the cleaning functionality based on your model.
You should inherit from ModelForm
from django.forms import ModelForm
class CategoryForm(ModelForm):
class Meta:
model = IngredienceCategory
Refer to https://docs.djangoproject.com/en/dev/topics/forms/modelforms/ for how to render form and save it to database.

ModelForm. Why my form is not filled with data from model?

Why my form is not filled with data from model?
This is my model.py
class People(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=100)
address = models.CharField(max_length=255)
This is my forms.py
from django.forms import ModelForm
class EditForm(ModelForm):
class Meta:
model = People
exclude=('user',)
views.py
def edit_data(request):
user = request.user
people = People.objects.get(user=user)
form = EditForm(request.POST, instance = people)
if request.method == 'POST':
if form.is_valid():
form.save()
else:
print 'Error'
else:
form = EditForm()
return render_to_response('profile.html',{'form':form}, context_instance=RequestContext(request))
profile.html
<form action="/profile/" method="post">{% csrf_token %}
{{ form.as_p }}
</form>
The problem is that you're redefining form in your else clause (to a new instance of your EditForm, which doesn't have the instance variable set). Remove the else (and the line under it) and you should be good to go.