简体   繁体   English

Django为什么要加载相同的页面,但使用不同的URL?

[英]Why is Django loading the same page, but with different URL?

I'm studying Django and doing a course to guide me in this process. 我正在学习Django,并正在通过课程指导我。 Now I'm building some forms and I got this problem: when I access http://127.0.0.1:8000/ the main page is loaded correctly, but when I click in the SignUp link, the page doesn't change, just the URL changes (now http://127.0.0.1:8000/signup ) with the same content of the homepage. 现在,我正在构建一些表单,但遇到了这个问题:当我访问http://127.0.0.1:8000/时 ,正确加载了主页,但是当我单击SignUp链接时,页面没有改变,只是URL更改(现在为http://127.0.0.1:8000/signup )具有与首页相同的内容。 I expected that the form was loaded and the template correspondent for this view as well. 我希望已加载表单,并且该视图也对应于模板。

I've checked if my code could be different from the original course code, but I found nothing wrong. 我检查了我的代码是否可能与原始课程代码不同,但是我没有发现任何错误。

Here is my views.py file: 这是我的views.py文件:

from django.shortcuts import render
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect

from .forms import SubscriberForm

def subscriber_new(request, template='subscribers/subscriber_new.html'):
    if request.method == 'POST':
        form = SubscriberForm(request.POST)
        if form.is_valid():
            # Unpack form values
            username = form.cleaned_data['username']
            password = form.cleaned_data['password1']
            email = form.cleaned_data['email']
            # Create the User record
            user = User(username=username, email=email)
            user.set_password(password)
            user.save()
            # Create Subscriber Record
            # Process payment (via Stripe)
            # Auto login the user
            return HttpResponseRedirect('/success/')
    else:
        form = SubscriberForm()

    return render(request, template, {'form':form})

This is my urls.py file: 这是我的urls.py文件:

from django.conf.urls import patterns, include, url
from marketing.views import HomePage
from django.contrib import admin

urlpatterns = patterns('',
    #url(r'^admin/', include(admin.site.urls)),

    # Marketing pages
    url(r'$', HomePage.as_view(), name="home"),

    # Subscriber related URLs
    url(r'^signup/$',
        'crmapp.subscribers.views.subscriber_new', name='sub_new'),
)

This is my forms.py file: 这是我的forms.py文件:

from django import forms
from django.contrib.auth.forms import UserCreationForm

class SubscriberForm(UserCreationForm):
    email = forms.EmailField(
        required=True, widget=forms.TextInput(attrs={'class':'form-control'})
    )
    username = forms.CharField(
        widget=forms.TextInput(attrs={'class':'form-control'})
    )
    password1 = forms.CharField(
        widget=forms.TextInput(attrs={'class':'form-control', 'type':'password'})
    )
    password2 = forms.CharField(
        widget=forms.TextInput(attrs={'class':'form-control', 'type':'password'})
    )

In my templates, the forms are called with this syntax: 在我的模板中,使用以下语法调用表单:

<li><a href="{% url 'sub_new' %}" class="p-r-none">Sign Up</a></li>

I'm using Django version 1.8.4 and Python 2.7.10. 我正在使用Django版本1.8.4和Python 2.7.10。

Can someone help me to understand what is happen, please? 有人可以帮我了解发生了什么吗?

您在"home"网址中缺少^

url(r'^$', HomePage.as_view(), name="home"),

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

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