如何呈现POST并使其显示在另一个页面上 [英] How to render a POST and make it show up on another page

查看:201
本文介绍了如何呈现POST并使其显示在另一个页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个类似于craigslist的市场网站。
我根据Django教程使用表单创建了一个表单,但我不知道如何从POST表单中获取信息。
我想从POST中获取的信息(主题,价格等)显示在另一个页面上。 http://bakersfield.craigslist.org/atq/3375938126.html ,而我想本产品的主题(请查看form.py)(例如,1906年法国椅子)在另一页上显示。 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):

如果request.method ==POST:
form = SellForm(request.POST)
如果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.clea ned_data ['body']

return HttpResponseRedirect('/ books /')

else:
form = SellForm()

render(request,'sell.html',{'form':form,})

◆网址。从

  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 =submitvalue =提交/>
< / form>


解决方案

我假设你有一个在您的数据库(存储用户的销售)中销售模型/表,否则不会有任何意义。这意味着您可以节省一些时间,并使用 ModelForm
而不是简单的表单。模型表单采用数据库表格并为其生成一个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>
<! - 其余的字段.. - >
< / div>
{%endblock%}

我们还需要一个新的URL条目来显示具体销售



urls.py


$ b import django.contrib中的$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
urlpatterns = patterns('',
#将`sell`视图更改为`sell_create`
url(r'^ sechand / $','site1.views.sell_create'),
#我们还添加一个卖的详细显示视图
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从您的app.forms导入Sell
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 = {}
如果request.method =='POST':
form = SellForm(request.POST)
如果form.is_valid():
# ModelForm的优点在于它知道如何在数据库中创建其底层模型的实例。
new_sell = form.save()#ModelForm.save()返回新创建的卖。
#我们立即将用户重定向到新的卖家的显示页面
return HttpResponseRedict('/ sechand /%d /'%new_sell.pk)
else:
form = SellForm )#在GET请求中,实例化一个空的表单来填写
context ['form'] = form
return render_to_response('sell.html',context)

这足以让你走。有些模式可以让这些东西更加模块化,更好,但是我不想让你有太多的信息,因为你是一个django初学者。


I'm trying to create a marketplace website similar to craigslist. I created a form according to the Django tutorial "Working with forms", but I don't know how to render information I got from the POST forms. I want to make information(subject,price...etc) that I got from POST show up on another page like this. http://bakersfield.craigslist.org/atq/3375938126.html and, I want the "Subject"(please look at form.py) of this product(eg.1960 French Chair) to show up on another page like this. http://bakersfield.craigslist.org/ata/ }

Can I get some advice to handle submitted information? Here's present codes. I'll appreciate all your answers and helps.

<-! Here's my codes -->

◆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>             

解决方案

I assume you have a Sell model/table in your db(where you store the users' "sells"), otherwise it wouldn't make any sense. This means you can save yourself some time and use a ModelForm, instead of a simple Form. A model form takes a database table and produces an html form for it.

forms.py

from django.forms import ModelForm
from yourapp.models import Sell

class SellForm(ModelForm):
    class Meta:
        model = Sell

In your views.py you need one more view that displays the Sells that your users have posted for others to see. You also need an html template that this view will render with context about each 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 %}

We also need a new url entry for the displaying of a specific 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)

This is enough to get you going I think. There are patterns to make these things more modular and better, but I don't want to flood you with too much information, since you are a django beginner.

这篇关于如何呈现POST并使其显示在另一个页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆