'Django form.save()
I have some problems with CRUD in Django. I can't add an object to the database.
File views.py
@login_required
def persons_add(request):
form = forms.PersonForm(request.POST)
if form.is_valid():
form.save()
return redirect('persons_list')
context = {
'form': form
}
return render(request, 'persons/persons_add.html', context)
File form.html
<form role="form" method="POST" action="/persons/">
{% csrf_token %}
<table class="table table-bordered">
{% for field in form.visible_fields %}
<tr>
<th>
{{ field.label }}
</th>
<td>
{{ field }}
</td>
</tr>
{% endfor %}
</table>
<button type="submit" name="submit" class="btn btn-success">Add Person</button>
</form>
File forms.py
from django import forms
from persons import models
class PersonForm (forms.ModelForm):
class Meta:
model = models.Person
fields = ['name', 'surname']
File person_list.html
<tbody>
{% for person in persons_list %}
<tr>
<td>{{ person.name }}</td>
<td>{{ person.surname }}</td>
<td>
<a href="{{ person.id }}/edit"
<p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-pencil"></span></button></p>
</a>
</td>
<td>
<a href="{{ person.id }}/delete">
<p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p>
</a>
</td>
</tr>
{% endfor %}
</tbody>
File urls.py (application)
from django.conf.urls import patterns, include, url
from django.contrib import admin
from persons import views
urlpatterns = patterns('',
#url(r'^$', views.PersonsView.as_view(), name='persons'),
url(r'^$', views.persons_list, name='persons_list'),
url(r'^add/', views.persons_add, name="persons_add"),
url(r'^(?P<person_id>\d+)/edit/$', views.persons_edit, name="persons_edit"),
url(r'^(?P<person_id>\d+)/delete/$', views.persons_delete, name="persons_delete"),
)
File urls.py (main project)
from django.conf.urls import patterns, include, url
from django.contrib import admin
from hr import views
urlpatterns = patterns('',
url(r'^persons/', include('persons.urls', namespace='persons')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^logout/$', 'django.contrib.auth.views.logout', {'template_name': 'login.html'}),
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
)
Where is the bug? Why is nothing working here? Why can't I send POST to database and add Person?
Solution 1:[1]
First of all, there are mistakes in your views. You need to send the empty form on a GET request, and handle the form when it's POST:
@login_required
def persons_add(request):
# Check if it's POST request then handle the form
if request.method == 'POST':
form = forms.PersonForm(request.POST)
if form.is_valid():
form.save()
return redirect('persons_list')
else:
# If not, you need an empty form for GET request
form = forms.PersonForm()
context = {
'form': form
}
return render(request, 'persons/persons_add.html', context)
Correct this first and then we will check again where you are, one step at a time.
Check your url route. You're currently posting to /persons/. Change to /persons/add/ as your url suggested.
Also, there is mistake in your main urls.py file. If you want to include application urls.py, do not mix with your main routes. Split them like this:
Main urls.py file:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^logout/$', 'django.contrib.auth.views.logout', {'template_name': 'login.html'}),
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
)
# Split your app URLs and using += patterns with their views
urlpatterns += patterns(
'persons.views',
(r'^persons/', include('persons.urls', namespace='persons')),
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Mortensen |
