我正在尝试创建类似于craigslist的市场网站。 我根据Django教程“使用表单”创建了一个表单,但我不知道如何呈现从POST表单中获取的信息。 我希望从POST获得的信息(主题,价格......等)显示在另一个页面上。 http://bakersfield.craigslist.org/atq/3375938126.html并且,我希望此产品的“主题”(请查看form.py)(例如,1960年法国主席)显示在此类其他页面上。 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>
答案 0 :(得分:2)
我假设您的数据库中存在Sell
模型/表(存储用户的“销售”),否则没有任何意义。这意味着您可以节省一些时间并使用ModelForm
,
而不是简单的Form
。模型表单获取数据库表并为其生成html表单。
<强> forms.py 强>
from django.forms import ModelForm
from yourapp.models import Sell
class SellForm(ModelForm):
class Meta:
model = Sell
在views.py中,您还需要一个显示用户所拥有的Sells
的视图
张贴给别人看。您还需要一个html模板,该视图将使用每个Sell
的上下文进行渲染。
<强> sell_display.html 强>
{% extends 'some_base_template_of_your_site.html' %}
{% block content %}
<div id="sell">
<h3> {{ sell.subject }}</h3>
<p> {{ sell.condition }}</p>
<p> {{ sell.body }}</p>
<!-- the rest of the fields.. -->
</div>
{% endblock %}
我们还需要一个新的网址条目来显示特定的Sell
<强> urls.py 强>
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Changed `sell` view to `sell_create`
url(r'^sechand/$','site1.views.sell_create'),
# We also add the detail displaying view of a Sell here
url(r'^sechand/(\d+)/$','site1.views.sell_detail'),
url(r'^admin/', include(admin.site.urls)),
)
<强> views.py 强>
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from yourapp.models import Sell
from yourapp.forms import SellForm
def sell_detail(request, pk):
sell = get_object_or_404(Sell, pk=int(pk))
return render_to_response('sell_display.html', {'sell':sell})
def sell_create(request):
context = {}
if request.method == 'POST':
form = SellForm(request.POST)
if form.is_valid():
# The benefit of the ModelForm is that it knows how to create an instance of its underlying Model on your database.
new_sell = form.save() # ModelForm.save() return the newly created Sell.
# We immediately redirect the user to the new Sell's display page
return HttpResponseRedict('/sechand/%d/' % new_sell.pk)
else:
form = SellForm() # On GET request, instantiate an empty form to fill in.
context['form'] = form
return render_to_response('sell.html', context)
这足以让你走了我想。有一些模式可以让这些东西更加模块化和更好,但是我不想给你太多信息,因为你是django的初学者。