如何使用夹层

时间:2014-04-01 08:08:35

标签: python django mezzanine

processor_for.py

from django import forms
from django.http import HttpResponseRedirect
from mezzanine.pages.page_processors import processor_for
from .models import Book

class BookForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()

@processor_for(Author)
def author_form(request, page):
    form = BookForm()
    if request.method == "POST":
        form =BookForm(request.POST)
        if form.is_valid():
            # Form processing goes here.
            redirect = request.path + "?submitted=true"
            return HttpResponseRedirect(redirect)
    return {"form": form}

models.py

from django.db import models
from time import time
class Book(models.Model):
   book_name= models.CharField(max_length=200, unique = True)
    def __unicode__(self):
        return self.book_name

views.py

def create_book (request):
    if request.POST:
        form = BookForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/all/')
    else:
        form = BookForm()
    args= {}
    args.update(csrf(request))
    args['form'] = form
    return render_to_response('create_Book.html', args)

urls.py

urlpatterns += patterns('',
    url(r'^/xyz/$', create_book))

create_Book.html

<form action="/xyz/" method="post" enctype="multipart/form-data">{% csrf_token %}
{{form.as_ul}}
<input type="submit" name="submit" value="create"/>
</form>

这就是我正在做的但仍然无法访问表单。我在哪里做错了。将不胜感激。请在代码中标记出错了什么?

2 个答案:

答案 0 :(得分:0)

根据您的代码和相关说明。请在urls.py中查看您的代码

urlpatterns += patterns('',
    url(r'^xyz/$', create_book))       # you should write like ^xyz/$

请关注django doc

答案 1 :(得分:0)

两个语法问题:

1)始终在&#34;中定义您的处理器名称。 &#34;比如@processor_for("Author")

2)页面处理器的网址,如:

 url("^xyz/$", "mezzanine.pages.views.page", {"slug": "Author"},  name="Author"),