为什么 Django 加载相同的页面,但 URL 不同?
Why is Django loading the same page, but with different URL?
我正在学习 Django 并开设了一门课程来指导我完成这个过程。现在我正在构建一些表单,但我遇到了这个问题:当我访问 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) 时具有与主页相同的内容。我预计表单已加载,模板也对应此视图。
我检查了我的代码是否与原始课程代码不同,但没有发现任何问题。
这是我的 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})
这是我的 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'),
)
这是我的 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'})
)
在我的模板中,使用以下语法调用表单:
<li><a href="{% url 'sub_new' %}" class="p-r-none">Sign Up</a></li>
我使用的是 Django 版本 1.8.4 和 Python 2.7.10。
有人可以帮助我了解发生了什么吗?
您在 "home"
中缺少 ^
url:
url(r'^$', HomePage.as_view(), name="home"),
我正在学习 Django 并开设了一门课程来指导我完成这个过程。现在我正在构建一些表单,但我遇到了这个问题:当我访问 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) 时具有与主页相同的内容。我预计表单已加载,模板也对应此视图。
我检查了我的代码是否与原始课程代码不同,但没有发现任何问题。
这是我的 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})
这是我的 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'),
)
这是我的 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'})
)
在我的模板中,使用以下语法调用表单:
<li><a href="{% url 'sub_new' %}" class="p-r-none">Sign Up</a></li>
我使用的是 Django 版本 1.8.4 和 Python 2.7.10。
有人可以帮助我了解发生了什么吗?
您在 "home"
中缺少 ^
url:
url(r'^$', HomePage.as_view(), name="home"),