2014-02-13 55 views
1

我正在通過Tango使用Django教程。我試圖讓用戶註冊的應用程序。但是,當用戶按下提交按鈕時,我收到以下錯誤消息:在Django中提交表單時未找到頁面(404)

Page not found (404) 
Request Method: POST 
Request URL: http://127.0.0.1:8000/rango/register/rango/register/ 
Using the URLconf defined in tango_with_django_project.urls, Django tried these URL patterns, in this order: 
^admin/ 
^rango/ ^$ [name='index'] 
^rango/ ^about$ [name='about'] 
^rango/ ^add_category/$ [name='add_category'] 
^rango/ ^category/(?P<category_name_url>\w+)/$ [name='category'] 
^rango/ ^category/(?P<category_name_url>\w+)/add_page/$ [name='add_page'] 
^rango/ ^register/$ [name='register'] 
media/(?P<path>.*) 
The current URL, rango/register/rango/register/, didn't match any of these. 

我不確定那個奇怪的路徑是如何構建的。這裏是登記模板:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Rango</title> 
    </head> 

    <body> 
    <h1>Register with Rango</h1> 

    {% if registered %} 
    Rango says: <strong>thank you for registering!</strong> 
    <a href="/rango/">Return to the homepage.</a><br /> 
    {% else %} 
    Rango says: <strong>register here!</strong><br /> 

    <form id="user_form" method="post" action="rango/register/" 
     enctype="multipart/form-data"> 

     {% csrf_token %} 

     {{ user_form.as_p }} 
     {{ profile_form.as_p }} 

     <input type="submit" name="submit" value="Register" /> 

    </form> 
    {% endif %} 

    </body> 

</html> 

爲了得到登記模板,你需要點擊索引模板此鏈接:

<a href="/rango/register">Register Here</a> 

這裏是蘭戈/ views.py寄存器功能:

def register(request): 
    registered = False 

    if request.method == 'POST': 
    user_form = UserForm(data=request.POST) 
    profile_form = UserProfileForm(data=request.POST) 

    if user_form.is_valid() and profile_form.is_valid(): 
     user = user_form.save() 

     user.set_password(user.password) 
     user.save() 

     profile = profile_form.save(commit=False) 
     profile.user = user 

     if 'picture' in request.FILES: 
     profile.picture = request.FILES['picture'] 

     profile.save() 

     registered = True 

    else: 
     print user_form.errors, profile_form.errors 

    else: 
    user_form = UserForm() 
    profile_form = UserProfileForm() 

    return render(request, 
    'rango/register.html', 
    {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}) 

我相信我錯過了一些小東西!

回答

4

不設置URL重寫節目前,您的瀏覽器將"rango/register/"到當前URL的末尾。 如果您將其更改爲"./""/rango/register/"它會指向自己,但這些不是最佳實踐。

對於最佳實踐使用{% url "register" %}代替,這樣它會自動更改爲你改變你的url.py

如:

<form id="user_form" method="post" action="{% url "register" %}" 
     enctype="multipart/form-data"> 
+1

如果您正在編輯的實例的對象,它會更好使用類似... action =「{%url'edit_author'author_id%}」...其中您的author_id可能已通過視圖傳遞給模板。 – Rexford

0

最有可能的<form action>屬性是錯誤的。

現在比較指出:

rango/register/rango/register/ 

    <form id="user_form" method="post" action="rango/register/" <--- relative 

對於一個快速的解決方法試試(不是最佳做法):

<form id="user_form" method="post" action="/rango/register/" 

代替。

瞭解相對URL如何在HTML頁面中工作,這應該解開這個謎團。

0

這似乎是問題是與你的URLs.py頁 - 好像你有Django的