简体   繁体   English

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

[英]Python Django 1.7 def(request) function- Post request keeps using same function

Here is the code that I am struggling with. 这是我正在努力的代码。 (I commented out the try so I could see what is happening). (我注释掉了尝试,以便可以看到发生了什么)。

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')

Here is the person page that I am struggling with. 这是我正在努力的人页面。

{% 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>

Then, when I try to get the data from the donation form the program calls back invalid literal for int() with base 10: '' and it refers to this line obj = get_object_or_404(Name, pk=unique_id) in the foo() function 然后,当我尝试从捐赠表格中获取数据时,程序以10为基数调用int()的无效文字:”,它在foo()中引用此行obj = get_object_or_404(Name,pk = unique_id)功能

My question is, why is it calling the same function? 我的问题是,为什么调用相同的函数? I'm trying to get it to call this function instead. 我试图让它代替调用此函数。

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')

Here is my urls.py for this section of the webpage. 这是我在网页此部分的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'),
)

Thank you so much for your attention! 非常感谢您的关注!

Thanks to karthikr I was able to find the answer. 多亏了karthikr,我才能找到答案。

The problem was in the urls.py modules. 问题出在urls.py模块中。 I had it originally set up like this. 我原来是这样设置的。

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'),
)

But had to update a '$' behind the '^' in order to end the url pattern. 但是必须更新“ ^”后面的“ $”以结束URL模式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM