0

我正在尝试创建一个类似于 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>             
4

1 回答 1

1

我假设您Sell的数据库中有一个模型/表(您存储用户的“销售”),否则它没有任何意义。这意味着您可以节省一些时间并使用ModelForm. 而不是简单的Form. 模型表单接受一个数据库表并为其生成一个 html 表单。

表格.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 %}

我们还需要一个新的 url 条目来显示特定的Sell

网址.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)),
)

视图.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 初学者。

于 2012-10-31T22:45:28.040 回答