Django form.save()

时间:2015-01-22 22:37:23

标签: python django django-forms

我在django中有一些CRUD问题,我无法将对象添加到数据库

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)

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>

forms.py

from django import forms

from persons import models

class PersonForm (forms.ModelForm):
    class Meta:
        model = models.Person
        fields = ['name', 'surname']

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>

urls.py(申请)

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"),
)

urls.py(主要项目)

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'}),
)

任何人都知道这个bug在哪里?为什么这里什么都没有?为什么我不能将POST发送到datebase并添加Person?

1 个答案:

答案 0 :(得分:2)

首先,您的views中存在错误,您需要在 GET 请求中发送空表单,并在 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)

首先纠正此问题,然后我们将再次检查您的位置,一次一步。

检查您的网址路线,您当前正在发布到/persons/,更改为/persons/add/,建议网址

此外,您的主urls.py中存在错误,如果您想要包含应用 urls.py ,请不要与您的主要路线混合,拆分他们是这样的:

main urls.py:

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')),
)