使用Django URL将发布的信息发送到另一个页面

时间:2016-12-16 14:51:35

标签: python django django-urls

我是Django的新手,我想知道如何使用网址(或任何其他方式)将信息从一个网页发送到另一个网页。我尝试制作一个全局变量并返回信息,但它没有起作用。在此示例中,我需要在按下提交按钮时将quote的发布数据发送到下一页。

这是我的模特:

from django.db import models

class OrderEntry(models.Model):
    quote_num = models.CharField(max_length=50, primary_key=True)
    customer = models.CharField(max_length=100)
    cust_po = models.CharField(max_length=50)
    datetime = models.DateTimeField(auto_now=True, editable=True)

形式:

from django import forms

class Details(forms.Form):
    quote_number = forms.CharField(max_length=50, min_length=3)
    customer = forms.CharField(max_length=100)
    po_number = forms.CharField(max_length=50, min_length=4)

查看:

from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from orders.models import OrderEntry, Items
from orders.forms import Details, ItemDetails

def entry(request):
if request.method == 'POST':
    form = Details(request.POST)
    if form.is_valid:
        customer = request.POST.get('customer')
        quote = request.POST.get('quote_number')
        purchase = request.POST.get('po_number')
        obj_oe = OrderEntry(customer = customer, quote_num = quote,
            cust_po = purchase)
        obj_oe.save()
        return HttpResponseRedirect('/entry/items')
else:
    form = Details(
    initial={'quote_number': 'QS', 'po_number': 'PO-'}
    )
return render(request, 'order.html', {'form': form})

def entry_items(request):
if request.method == 'POST':
    form = ItemDetails(request.POST)
    if form.is_valid:
        # quote = quote value
        a1 = request.POST.get('i1')
        a2 = request.POST.get('i2')
        a3 = request.POST.get('i3')
        a4 = request.POST.get('i4')
        a5 = request.POST.get('i5')
        a6 = request.POST.get('i6')
        a7 = request.POST.get('i7')
        a8 = request.POST.get('i8')
        a9 = request.POST.get('i9')
        a10 = request.POST.get('i10')
        obj_oi = Items(quote_num = quote, item_1 = a1, item_2 = a2, item_3 = a3, item_4 = a4,
            item_5 = a5, item_6 = a6, item_7 = a7, item_8 = a8, item_9 = a9,
            item_10 = a10)
        obj_oi.save()
        return HttpResponseRedirect('/entry')
else:
    form = ItemDetails
return render(request, 'order_items.html', {'form': form})

最后,网址:

from django.conf.urls import url
from django.contrib import admin
from orders import views_orders

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^entry/$', views_orders.entry),
url(r'^entry/items', views_orders.entry_items),
]

那么如何在下一页中将quote的值转换为变量?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

试试这个

HttpResponseRedirect(reverse('namespace:name', args=(var_to_send,)))