在我的 Django 项目中,我有两个相似的 url
In my Django project I have two similar url
我对我的网站 url 有疑问。当有人想去 mysite.com 时,我将他们重定向到 mysite.com/register.
url(r'^$', RedirectView.as_view(url='register/', permanent=False), name='index'),
url(r'^register/',views.guaform2,name='custform2'),
我还有另一个 url 允许在寄存器部分之后写一些东西。例如,当有人访问这个网站 mysite.com/register/kh-54-m2-fc 它允许访问:
url(r'^register/(?P<pk>[-\w]+)',views.guaform,name='custform'),
但是当我同时使用这些 url 时,我的 guaform 视图不起作用。它转到 customer_form2.html。当有人访问此站点时:mysite.com/register/ 我想让他们访问的任何内容都可以访问 customer_form.html。我怎样才能将这些 url 分开?
urls.py
from django.conf.urls import url
from django.contrib import admin
from olvapp import views
from django.views.generic import RedirectView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', RedirectView.as_view(url='register/', permanent=False), name='index'),
url(r'^register/',views.guaform2,name='custform2'),
url(r'^register/(?P<pk>[-\w]+)',views.guaform,name='custform'),
]
views.py
from django.shortcuts import render,HttpResponse
from olvapp.models import Customer
from olvapp.forms import CustomerForm,CustomerForm2
from django.conf import settings
from django.http import HttpResponseRedirect
from django.urls import reverse
from olvapp import forms
def guaform(request,pk):
theurl = request.get_full_path()
orderid = theurl[10:]
form = CustomerForm(initial={'order_id':orderid})
if request.method == "POST":
form = CustomerForm(request.POST)
if form.is_valid():
form.save(commit=True)
return HttpResponseRedirect(reverse('thank'))
else:
HttpResponse("Error from invalid")
return render(request,'customer_form.html',{'form':form})
def guaform2(request):
form = CustomerForm2()
if request.method == "POST":
form = CustomerForm2(request.POST)
if form.is_valid():
form.save(commit=True)
return HttpResponseRedirect(reverse('thank'))
else:
HttpResponse("Error from invalid")
return render(request,'customer_form2.html',{'form':form})
锁定正则表达式应该就足够了,因此它在两种情况下都不匹配。
通过在末尾使用美元符号来标记输入的结尾来加强 custform2 正则表达式:
url(r'^register/$',views.guaform2,name='custform2'),
这应该足以让事情正常进行
我对我的网站 url 有疑问。当有人想去 mysite.com 时,我将他们重定向到 mysite.com/register.
url(r'^$', RedirectView.as_view(url='register/', permanent=False), name='index'),
url(r'^register/',views.guaform2,name='custform2'),
我还有另一个 url 允许在寄存器部分之后写一些东西。例如,当有人访问这个网站 mysite.com/register/kh-54-m2-fc 它允许访问:
url(r'^register/(?P<pk>[-\w]+)',views.guaform,name='custform'),
但是当我同时使用这些 url 时,我的 guaform 视图不起作用。它转到 customer_form2.html。当有人访问此站点时:mysite.com/register/ 我想让他们访问的任何内容都可以访问 customer_form.html。我怎样才能将这些 url 分开?
urls.py
from django.conf.urls import url
from django.contrib import admin
from olvapp import views
from django.views.generic import RedirectView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', RedirectView.as_view(url='register/', permanent=False), name='index'),
url(r'^register/',views.guaform2,name='custform2'),
url(r'^register/(?P<pk>[-\w]+)',views.guaform,name='custform'),
]
views.py
from django.shortcuts import render,HttpResponse
from olvapp.models import Customer
from olvapp.forms import CustomerForm,CustomerForm2
from django.conf import settings
from django.http import HttpResponseRedirect
from django.urls import reverse
from olvapp import forms
def guaform(request,pk):
theurl = request.get_full_path()
orderid = theurl[10:]
form = CustomerForm(initial={'order_id':orderid})
if request.method == "POST":
form = CustomerForm(request.POST)
if form.is_valid():
form.save(commit=True)
return HttpResponseRedirect(reverse('thank'))
else:
HttpResponse("Error from invalid")
return render(request,'customer_form.html',{'form':form})
def guaform2(request):
form = CustomerForm2()
if request.method == "POST":
form = CustomerForm2(request.POST)
if form.is_valid():
form.save(commit=True)
return HttpResponseRedirect(reverse('thank'))
else:
HttpResponse("Error from invalid")
return render(request,'customer_form2.html',{'form':form})
锁定正则表达式应该就足够了,因此它在两种情况下都不匹配。
通过在末尾使用美元符号来标记输入的结尾来加强 custform2 正则表达式:
url(r'^register/$',views.guaform2,name='custform2'),
这应该足以让事情正常进行