我正在尝试将django-respite与 Django 一起使用,但我在创建新对象时遇到了问题。
在正常的 Django 视图中,我会
def post_new(request):
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect("/posts/")
else:
form = PostForm()
ctx = {'form': form}
return render_to_response('app_post/new_post.html', ctx, RequestContext(request))
我试图将它移植到喘息中(文档只讨论 GET 方法),我得出了这个结论:
# views.py
class PostViews(Views):
supported_formats = ['html', 'json']
template_path = 'post/'
def index(self, request):
...
def show(self, request, id):
...
def create(self, request):
form = PostForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect("posts/")
return self._render(
request = request,
template = 'new_post',
context = {
'form': form
},
)
def new(self, request):
form = PostForm()
return self._render(
request = request,
template = 'new_post',
context = {
'form': form
},
)
# urls.py
urlpatterns = resource(
prefix = 'posts/',
views = PostViews,
routes = [
routes.route(
regex = r'^(?:$|index%s$)' % (templates.format),
view = 'index',
method = 'GET',
name = 'posts'
),
routes.route(
regex = r'^(?P<id>[0-9]+)%s$' % (templates.format),
view = 'show',
method = 'GET',
name = 'post'
),
# New
routes.route(
regex = r'^(?:$|new%s$)' % (templates.format),
view = 'new',
method = 'GET',
name = 'post_new'
),
# Create
routes.route(
regex = r'^(?:$|create%s$)' % (templates.format),
view = 'create',
method = 'POST',
name = 'post_create'
)])
但是当我试图去
http://localhost:8000/posts/new.html
我明白了
'PostViews' object has no attribute 'new'
有人可以向我解释如何暂时管理 POST 和表单吗?