Save data to database using Django forms - python

my Save button does not save the user entered data into the database in a django form, where is the problem?
models.py
class Cemetery(models.Model):
id = models.AutoField(primary_key=True)
name=models.CharField(verbose_name="Cemetery Name",max_length=100)
city=models.CharField(max_length=30)
zipcode=models.CharField(max_length=5)
date_created=models.DateTimeField(editable=False, auto_now_add=True)
date_modified= models.DateTimeField(editable=False, auto_now=True)
created_by=models.ForeignKey('auth.User')
def __str__(self):
return str(self.id) +'-' + self.name + ' - ' + self.city
forms.py
class CemeteryForm(forms.ModelForm):
class Meta:
model=Cemetery
fields=('name','city','zipcode',)
views.py
def cemetery_add(request):
if request.method=="POST":
form=CemeteryForm(request.POST)
if form.is_valid():
cemetery=form.save(commit=False)
cemetery.name=request.name
cemetery.city=request.city
cemetery.zipcode=request.zipcode
cemetery.created_by=request.user
cemetery.date_created=timezone.now()
cemetery.save()
return redirect('cemetery_list')
else:
form=CemeteryForm
return render(request,'heaven/edit_cemetery.html',{'form':form})
template
{% extends 'heaven/base.html' %}
{% block content %}
<!-- Edit Cemetery -->
<h2>New Cemetery</h2>
<form method="POST" class="cemetery-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}
when I push the save button, this is the error I receive:
AttributeError at /cemetery_add/
'WSGIRequest' object has no attribute 'name'

In your form, you're looking up values directly on the request
request.name
You need to look in the form's cleaned data
form.cleaned_data.get('name')
Although, this isn't actually needed since the value is already set from the bound form.
If you really want to use the post data then it would be found in the POST dictionary
request.POST.get('name')
In short, ignore resetting these values and just add the fields you need
cemetery=form.save(commit=False)
cemetery.created_by=request.user
cemetery.date_created=timezone.now()
cemetery.save()

This one worked, I had an authentication problem, since my login session was expired I could not save:
def cemetery_add(request):
if request.method=='POST':
form=CemeteryForm(request.POST)
if form.is_valid():
cemetery=form.save(commit=False)
cemetery.name=request.POST.get('name')
cemetery.city=request.POST.get('city')
cemetery.zipcode=request.POST.get('zipcode')
cemetery.date_created=timezone.now()
if request.user.is_authenticated:
cemetery.created_by=request.user
else:
return login(request)
cemetery.save()
return redirect('cemetery_list')
else:
form=CemeteryForm
return render(request,'heaven/edit_cemetery.html',{'form':form})

Related

Django adding to database with foreign key while still showing the informations from the other model

I want to add elements to my database (for model Student) while having stuff from another model (School) be displayed alongside the form for the Student.\
This is the models.py
class School(models.Model):
name = models.CharField(max_length=256)
principal = models.CharField(max_length=256)
location = models.CharField(max_length=256)
def get_absolute_url(self):
return reverse('basiccbv:detail', kwargs={'pk':self.pk})
def __str__(self):
return self.name
class Student(models.Model):
name = models.CharField(max_length=256)
age = models.PositiveIntegerField(validators= [validators.MinValueValidator(1),validators.MaxValueValidator(20)],default=1)
school = models.ForeignKey(School, related_name='students')
def __str__(self):
return self.name
In my views.py I have this:
class SchoolDetailedView(DetailView):
context_object_name = 'school_detail'
model = models.School
template_name = 'basiccbv/school_detail.html'
# What i want is when I visit the link in the description I want to
# to see the school stuff and the form to add the student in this new
# view
class StudentCreateView(CreateView):
model = models.School
# I tried using the Student but that I don't know how to display the
# school information, I tried with related_name = 'students' but it
# didn't work(I don't know if that can be done the way that intended it
# or I just don't have the knowledge )
fields = ['name', 'age']
# If I use School I could get the name of the school in the title and
# its primary key, but than I don't know how to display the form and
# vise versa
template_name = 'basiccbv/student_update.html'
This is the .html file that gets me to the page where I need the form.
The link is the one calling 'basiccbv:studentupdate'
The related_name students was used here but I still can't figure out if it can
be done for adding stuff the way I want
<h1>Welcome to the school details page</h1>
<h2>School details:</h2>
<p>Name: {{ school_detail.name }}</p>
<p>Principal: {{ school_detail.principal }}</p>
<p>Location: {{ school_detail.location }}</p>
<h3>Students:</h3>
{% for student in school_detail.students.all %}
<p>{{ student.name }} who is {{ student.age }} years old.</p>
{% endfor %}
<div class="container">
<p><a href="{% url 'basiccbv:studentupdate' pk=school_detail.pk %}">Add a
student</a></p>
And here is the .html file with the form
## I changed this part bellow but nothing worked
<h1>Student coming to {{ student.school.name }}</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Add student">
</form>
I'm really stuck and can't find any information about this but if you can help me or give any advice thank you.
The way I used to add students was with admin and for schools I used admin until I created the view for creating Schools which worked with no problems(probably because there were no foreign keys).
I think you can take this approach
Forms:
# We need to define a new form first
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = ['name', 'age']
Views:
# we are using form view for using the form mentioned above
class StudentCreateView(FormView):
form_class = StudentForm
success_url = "/"
def get(self, request, school_id, **kwargs):
context = self.get_context_data(**kwargs) # getting context, ie: the form
context[school] = School.objects.get(pk=school_id) # updating the context with school object using the PK provided with the url
return self.render_to_response(context)
def post(self, request, school_id, **kwargs):
# overriding default implementation
form = self.get_form()
if form.is_valid():
return self.form_valid(form, school_id) # passing the pk value to form valid function to override
else:
return self.form_invalid(form)
def form_valid(self, form, school_id):
# overriding default implementation
self.object = form.save(commit=False)
self.object.school = School.objects.get(id=school_id) # saving the school information to the object
self.object.save()
return super(StudentCreateView, self).form_valid(form)
Template
# template
<h1>Student coming to {{ school.name }}</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Add student">
</form>
Urls
path('school/student-update/<int:school_id>/', StudentCreateView.as_view(), name='studentupdate'),

form.is_valid() is returning False when using ModelForm

I am creating a form using ModelForm to let the users upload a file along with a description . The is_valid() function isn't returning true and I am really confused. I have searched and there are many questions with same title as mine but they don't solve my problem.
here is forms.py:
class PostForm(forms.ModelForm):
document = forms.FileField(widget=forms.FileInput)
class Meta:
model = FeedModel
fields = ['description', 'document']
Here is models.py:
class FeedModel(models.Model):
description = models.CharField(max_length=255, blank=True)
document = models.FileField()
like = models.IntegerField(default=0)
dateTime = models.DateTimeField(auto_now=True, auto_created=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, default=0)
def get_absolute_url(self):
u=self.user
return reverse('home:feed',u.primary_key)
Here is views.py:
class PostView(CreateView):
form_class = PostForm
template_name = 'home/feedModel_form.html'
def get(self, request, *args, **kwargs):
form=self.form_class(None)
return render(request, self.template_name, {'form':form })
def post(self, request, *args, **kwargs):
logger = logging.getLogger(__name__)
form=self.form_class(request.POST)
if form.is_valid():
user=request.user
self.object=form.save(commit=False)
self.object.user=user
self.object.save()
logger.error("voila")
redirect({'home:feed'}, user.id)
return render(request, self.template_name, {'form':form })
def feedview(request, user_id):
user = User.objects.get(pk=user_id)
return render(request, 'home/feed.html', {'user': user})
Here is feedModel_form.html:
{% extends 'home/navbar.html' %}
{% block body %}
<div class="form">
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'home/form.html' %}
<button type="submit" class="button button-block" `
name="reg_btn">Post</button>`
</form>
</div>
{% endblock %}
Here is form.html:
{% for field in form %}
<div class="field-wrap">
<label>
{{ field.label_tag }}<span class="req">*</span>
</label>
<div>{{ field }}</div>
</div>
{% endfor %}
To see why the form isn't valid, you should check form.errors.
One error will be because you have not passed request.FILES to the form.
form=self.form_class(request.POST, request.FILES)
There may be other errors as well. If you used {{ form }} in your template, Django would include the errors automatically. Since you are rendering the fields manually, it's up to you to include the errors.
The key problem here is that you have overridden post. That means that you're missing out on lots of the code from CreateView.
In your case, it looks like you could remove the post method, and simply override form_valid instead.
def form_valid(self, form):
self.object=form.save(commit=False)
self.object.user=user
self.object.save()
# Note that you had {'home:feed'} here which was incorrect
return redirect('home:feed', user_id)
Your document field expects an uploaded file and is required. In order for the form to actually get the file, you have to also pass it the uploaded file in views.py:
form = self.form_class(data=request.POST, files=request.FILES)

Submitting a model class from view.py

Ive tried to submit a model class using a view function but the form didn't submitted.
This The model class inside models.py file :
class Campaign(models.Model):
username = models.OneToOneField(User, unique=True)
title = models.CharField(max_length=200)
message = models.TextField()
link = models.CharField(max_length=200)
added= models.DateTimeField(auto_now_add=True)
and this its form inside forms.py :
class CampaignForm(forms.ModelForm):
class Meta:
model = Campaign
fields=('title','message')
Ive tried to submit this form using this function at views.py :
def Campaign(request):
form = CampaignForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
submit= Campaign(username=request.user)
submit.save()
context={
'form':form,
}
return render(request, 'campaigns.html',context)
And this is the html file :
{% extends "base.html" %}
{% block content %}
<form action="" method="POST" class="">
{% csrf_token %}
{{form.as_p}}
<input type="submit">
</form>
{% endblock content %}
You have to do action="#" in your form html because some browsers can not accept empty action values.
Also you have a mistake in this part of code:
submit= Campaign(username=request.user)
submit.save()
You have to get the form values into your object, you can do it this way:
submit = form.save(commit=False)
submit.username = request.user
submit.save()
Some browsers will fail to post if the action attribute is empty, try with
<form action="." method="POST">
Notice the dot ".", since you are using the same view to display the form and process it the dot it's ok.

FileField not uploading the file

I am relatively new to python and Django.
The form is displaying itself, but when i choose a file and hit save, the form just refreshes itself and the selected file in the FormField disappears and the forms says that this filed is required.
The code is :
models.py
class ScribbleMedia(models.Model):
media = models.FileField(upload_to='photos')
def __unicode__(self):
return self.media
class Scribble(models.Model):
body = models.TextField()
user = models.ForeignKey(User)
media = models.ForeignKey(ScribbleMedia)
def __unicode__(self):
return u'%s, %s' % (self.user.username, self.media)
class Tag(models.Model):
name = models.CharField(max_length=64, unique= True)
scribbles = models.ManyToManyField(Scribble)
def __unicode__(self):
return self.name
views.py
def scribbler_save_page(request):
if request.method == 'POST':
form = ScribbleSaveForm(request.POST)
if form.is_valid():
#Create or get media.
media, dummy = ScribbleMedia.objects.get_or_create(
media=form.cleaned_data['media']
)
#Create or get media.
scribble, created=Scribble.objects.get_or_create(
user=request.user,
media=media
)
#update scribble body
scribble.body=form.cleaned_data['body']
# If the scibble is being updated, clear old tag list.
if not created:
scribble.tag_set.clear()
# Create new tag list.
tag_names = form.cleaned_data['tags'].split()
for tag_name in tag_names:
tag, dummy = Tag.objects.get_or_create(name=tag_name)
scribble.tag_set.add(tag)
# Save scribble to database.
scribble.save()
return HttpResponseRedirect ( '/user/%s/' % request.user.username
)
else:
form = ScribbleSaveForm()
variables = RequestContext (request, {
'form': form
})
return render_to_response ('scribble_save.html', variables)
forms.py
class ScribbleSaveForm(forms.Form):
media=forms.FileField(
label=u'add file',
widget=forms.FileInput()
)
text=forms.CharField(
label=u'description',
widget=forms.Textarea()
)
tags=forms.CharField(
label=u'Tags',
required=False,
widget=forms.TextInput(attrs={'size':64})
)
urls.py
(r'^save/$', scribbler_save_page),
scribble_save.html
{% extends "base.html" %}
{% block title %}Save Scribble {% endblock %}
{% block head %}Save Scribble { % endblock % }
{% block content %}
<form method="post" action=".">
{{ form.as_p }}
<input type="submit" value="save" />
{% csrf_token %}
</form>
{% endblock %}
Two things you need to do specially for file uploads:
(1) Your HTML form element must include the attribute enctype with the value multipart/form-data.
<form enctype="multipart/form-data" method="post" action=".">
(2) In your view, you'll find uploaded files in request.FILES instead of request.POST. To bind uploaded files to your form, pass in request.FILES as the second argument to the form constructor.
form = ScribbleSaveForm(request.POST, request.FILES)
Source: Django docs - The Forms API - Binding uploaded files to a form

Break up several actions into multiple view functions

I have one large view function where a user can Add, Edit, Delete, and Update his education. I am currently doing this all in one view because I haven't yet learned how to split up views by function. Here is what I currently have --
I have a single URL pointing to the view --
url(r'^profile/edit/education/$', 'views.edit_education', name='edit_education')
Here is my model/modelform --
class Education(models.Model):
school = models.CharField(max_length=100)
class_year = models.IntegerField(max_length=4, blank=True, null=True, choices=YEAR)
degree = models.CharField(max_length=100, blank=True)
user = models.ForeignKey('UserProfile')
class EducationForm(ModelForm):
class Meta:
model = Education
exclude = ('user',)
Here is my view --
#login_required
def edit_education(request, edit=0):
"""
In the edit profile page, allows a user to edit his education
and add multiple school entries.
"""
profile = request.user.get_profile()
education = profile.education_set.order_by('-class_year') # for the template. display all eduation entries
# unindented for legibility
if request.method == 'POST':
if 'Add School' in request.POST.values():
form = EducationForm(data=request.POST, request=request) # passing request to form to do validation based on request.user
if form.is_valid():
new_education = form.save(commit=False)
new_education.user = profile
new_education.save()
return redirect('edit_education')
if 'Delete' in request.POST.values():
for education_id in [key[7:] for key, value in request.POST.iteritems() if key.startswith('delete')]:
Education.objects.get(id=education_id).delete()
return redirect('edit_education')
if 'Edit' in request.POST.values():
for education_id in [key[5:] for key, value in request.POST.iteritems() if value == 'Edit' and key.startswith('edit')]:
edit = 1
school_object = Education.objects.get(id = education_id)
form = EducationForm(instance = school_object, request=request)
return render_to_response('userprofile/edit_education.html', {'form': form, 'education':education, 'edit': edit, 'education_id': education_id}, context_instance=RequestContext(request))
if 'Cancel' in request.POST.values():
return redirect('edit_education')
if 'Save Changes' in request.POST.values():
form = EducationForm(request.POST, request=request, edit=1)
if form.is_valid():
Education.objects.get(id=request.POST['education_id']).delete() # is there a way to update instead of delete and re-add?
new_education = form.save(commit=False)
new_education.user = profile
new_education.save()
return redirect('edit_education')
else:
form = EducationForm(request=request)
return render_to_response('userprofile/edit_education.html', {'form': form, 'education': education, }, context_instance=RequestContext(request))
And finally, my template --
<h3>Edit education info for {{user.get_full_name}}</h3>
<form action="." method="post"> {% csrf_token %}
{% if education %}
{% for education in education %}
<p><b>{{ education.school }}</b> {% if education.class_year %}{{ education.class_year|shorten_year}}, {% endif %} {{ education.degree}}
<input type="submit" name="edit_{{education.id}}" value='Edit' />
<input type="submit" name="delete_{{education.id}}" value="Delete" /></p>
{% endfor %}
{% endif %}
<table>
<input type="hidden" name="education_id" value="{{education_id}}" />
<tr><td>School:</td><td>{{form.school}}{{form.school.errors}}</td></tr>
<tr><td>Class Year</td><td>{{form.class_year}}{{form.class_year.errors}}</td></tr>
<tr><td>Degree:</td><td>{{form.degree}}{{form.degree.errors}}</td></tr>
<tr>{{form.non_field_errors}}</tr>
</table>
{% if not edit %}
<p><input type="submit" name="add" value="Add School" ></p>
{% else %}
<p><input type="submit" name="save" value="Save Changes" >
<input type="submit" name="cancel" value="Cancel" ></p>
{% endif %}
</form>
And the end is here. How would I separate one of these actions in the view into separate view functions using separate URLs? One or two examples would be more than enough. Thank you very much for your help.
A few ideas:
You could split your one big html form element into chunks
You could use AJAX submit handler to change URL based on pressed submit button
You could do what user Cerales suggested, but instead of redirecting which loses POST data you could just call add_school() and other methods, possibly having also dictionary map of actions mapped to their handlers: action_map = {'Add School': add_school, ...} - this would eliminate chain of conditions
You could use class-based view which would be basically a class-based version of #3. Django docs for generic class-based views are here
I can elaborate on any of those ideas if you will.
--
EDIT:
Answering your question from comments:
from django.views.generic.base import View
class MySchoolView(View):
def post(self, request, *kargs, **kwargs):
if 'Add School' in request.POST:
return self.add_school(request, *kargs, **kwargs)
# (...)
def add_school(self, request, *kargs, **kwargs):
# (...)
Then in urls.py:
(r'^schools/add/$', MySchoolView.as_view())
Note that the above is not tested so might require some tweaks to work. View class source code is here.
There's a couple of ways you could do this.
This could be a part of your view:
if request.method == 'POST':
if 'Add School' in request.POST.values():
return HttpResponseRedirect('/add_school/')
Then this could be part of another view, corresponding with the /add_school/ url:
def add_school(request):
if request.method=='POST':
form = EducationForm(data=request.POST, request=request) # passing request to form to do validation based on request.user
if form.is_valid():
new_education = form.save(commit=False)
new_education.user = profile
new_education.save()
return redirect('edit_education')

Categories

Resources