Python Django 1.7 def(请求)函数 - Post请求继续使用相同的函数

时间:2015-04-06 14:52:35

标签: python django

这是我正在努力解决的代码。 (我注释了这个尝试,所以我可以看到发生了什么)。

def foo(request):
#try:
    if request.method == "POST":
        unique_id = request.POST.get('def_id', '')
        password = request.POST.get('pwd', '')
        obj = get_object_or_404(Name, pk=unique_id)
        pwd = get_object_or_404(BAR, location_id=unique_id, password=password)
        return render(request, 'details/person.html', {'obj': obj, 'pwd':pwd})
#except:
#    wrong = 'The ID and Password did not return any results'
#    return render(request, 'details/index.html', {'wrong':wrong})
    else:
        return render(request, 'details/index.html')

这是我正在努力解决的人物页面。

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'foo/style.css' %}" />

<body id="generic" class>
<div role="banner" id="top">
    <h1 align="center"><font size="30">BAZ</font></h1>
</div>
<div role="banner" id="undertop">
    <h4 align="center">Effectively Crowd-Sourcing FOO</h2>
</div>
<div align='center'>
<font id="goo" size="20" color="green"><b>FOO</b></font><br>
<font size="20"><b>{{ obj }}</b></font><br><br>
<font id="gar" size="20" color="green"><b>BAR</b></font><br>
<font size="10" color="red">{{ pwd }}</font><br><br>
<hr>
<p><b>If you would like to donate please fill the information found in this form.<br>Feel free to remain anonymous and leave a comment.</b></p><br>
<form action="{% url 'details:donor' %}" method="post">
    {% csrf_token %}
    First Name<br>
    <input type="text" name="first_Name"><br>
    Last Name<br>
    <input type="text" name="last_Name"><br>
    Donation<br>
    <input type="number" placeholder="00.00" name="donation"><br>
    Comments<br>
    <textarea rows="12" cols="60" maxlength="1000" name="first_Name"></textarea><br>
    <input type="submit" value="Submit">
</form>
<hr>

然后,当我尝试从捐赠表单中获取数据时,程序将使用基数10调用int()的无效文字:&#39;&#39; 它指的是这条线     obj = get_object_or_404(名称,pk = unique_id) 在foo()函数中

我的问题是,为什么它调用相同的功能?我试图让它来调用这个函数。

def donor(request):
    if request.method == "POST":
        first_Name = request.POST.get('first_Name','')
        last_Name = request.POST.get('last_Name','')
        donation = request.POST.get('donation','')
        comments = request.POST.get('comments','')
        goodies = Donor(donor_First_Name=first_Name, donor_Last_Name=last_Name, donation_Amount=donation, comment=comments, person_id=obj_id)
        goodies.save()
        return render(request, 'details/donor.html')
    return render(request, 'details/donor.html')

以下是此网页部分的urls.py.

from django.conf.urls import patterns, url

from details import views

urlpatterns = patterns('',
    url(r'^', views.foo, name='foo'),
    url(r'^pledge/', views.donor, name='donor'),
)

非常感谢你的关注!

1 个答案:

答案 0 :(得分:0)

感谢karthikr,我找到了答案。

问题出在urls.py模块中。我最初设置的就像这样。

from django.conf.urls import patterns, url

from details import views

urlpatterns = patterns('',
url(r'^', views.foo, name='foo'),
url(r'^pledge/', views.donor, name='donor'),
)

但必须更新一个&#39; $&#39; &#39; ^&#39;为了结束网址模式。

urlpatterns = patterns('',
url(r'^$', views.foo, name='foo'),
url(r'^pledge/', views.donor, name='donor'),
)