我正在尝试创建一个类似于 craigslist 的市场网站。我根据 Django 教程“使用表单”创建了一个表单,但我不知道如何呈现从 POST 表单中获得的信息。我想让我从 POST 获得的信息(主题、价格...等)显示在这样的另一个页面上。http://bakersfield.craigslist.org/atq/3375938126.html并且,我希望这个产品(例如 1960 年法国椅)的“主题”(请查看 form.py)显示在这样的另一个页面上。http://bakersfield.craigslist.org/ata/ }
我可以得到一些建议来处理提交的信息吗?这是目前的代码。我将感谢您的所有回答和帮助。
<-!这是我的代码-->
◆forms.py
from django import forms
class SellForm(forms.Form):
subject = forms.CharField(max_length=100)
price = forms.CharField(max_length=100)
condition = forms.CharField(max_length=100)
email = forms.EmailField()
body = forms.TextField()
◆views.py
from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect
from site1.forms import SellForm
def sell(request):
if request.method =="POST":
form =SellForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
price = form.cleaned_data['price']
condition = form.cleaned_data['condition']
email = form.cleaned_data['email']
body = form.cleaned_data['body']
return HttpResponseRedirect('/books/')
else:
form=SellForm()
render(request, 'sell.html',{'form':form,})
◆urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^sechand/$','site1.views.sell'),
url(r'^admin/', include(admin.site.urls)),
)
◆sell.html
<form action = "/sell/" method = "post">{% csrf_token%}
{{ form.as_p }}
<input type = "submit" value="Submit" />
</form>