[英]Django : URL is not serving the correct page
Im learning django and in my apps, I have a urls.py which has 2 urlpatterns in it. 我正在学习Django,在我的应用中,我有一个urls.py ,其中包含2个urlpatterns 。 No matter what ever url I use in browser, it redirects to first url only
无论我在浏览器中使用什么url,它都只会重定向到第一个url
My urls 我的网址
urlpatterns = [
url(r'^', views.index, name='index'),
url(r'^contact/', views.contact, name='contact')
]
my views.py 我的views.py
def index(request):
title = 'welcome'
form = SignupForm(request.POST or None)
context={'title':title,'form':form}
if form.is_valid():
instance=form.save(commit=False)
email= form.cleaned_data['email']
email_save,created=Signup.objects.get_or_create(email=email)
return render(request,'index.html',context)
def contact(request):
contact_info=ContactForm(request.POST or None)
if contact_info.is_valid():
contact_info.save(commit=False)
print contact_info.cleaned_data['name']
return render(request,'contact.html',{'contact_info':contact_info})
My form 我的表格
class SignupForm(forms.ModelForm):
class Meta:
model = Signup
fields = ['fullname','email']
class ContactForm(forms.Form):
name=forms.CharField(max_length=128, required=True,)
email=forms.EmailField(required=True)
phone=forms.IntegerField(max_value=10)
message=forms.TextInput()
as per my contact url it should give me contact.html , but it renders only index.html 根据我的联系人网址,它应该给我contact.html ,但它仅呈现index.html
What could be the issue? 可能是什么问题?
You need to change the first redirection line to 您需要将第一个重定向行更改为
url(r'^$', views.index, name='index')
because contact page url pattern contains ^
(url pattern of index page ) which redirects you to the index page. 因为联系页面的网址格式包含
^
(索引页的网址格式),它将您重定向到索引页。 If you change like above, index
page should be shown only when the url string is followed by an empty string. 如果您进行上述更改,则只有在url字符串后跟一个空字符串时,才应显示
index
页。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.