简体   繁体   中英

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. 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. 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:

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:

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:

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.

Can someone help me to understand what is happen, please?

您在"home"网址中缺少^

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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