我想从之前的网址获取pk,例如:
/instructor/1
/instructor/1/sex/2
问题是,当选择性别时,inn URL.py会显示一个新的URL,我无法保存教师的PK。
这是URL.py
from django.conf.urls import url, include
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', views.InstructorListView.as_view(), name='instructor_list'),
url(r'^instructor/(?P<pk>[\w-]+)$', views.SexoListView.as_view(), name='sexo_list'),
url(r'^instructor/(?P<pk>[\w-]+)/sexo/(?P<pk2>[\w-]+)$', views.RutinaListView.as_view(), name='rutina_list'),
url(r'^detail/(?P<pk>\d+)$', views.InstructorDetailView.as_view(), name='instructor_detail'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是views.py
from django.shortcuts import render
from rutinas.models import Instructor, Rutina, Sexo
from django.views.generic import ListView, DetailView
from django.shortcuts import get_object_or_404
from django.template import loader
from django.http import HttpResponse
# Create your views here.
class InstructorListView(ListView):
model = Instructor
class InstructorDetailView(DetailView):
model = Instructor
class RutinaListView(ListView):
model = Rutina
# Filtro de rutinas deacuerdo a la seleccion de instructor y sexo
def get_queryset(self):
inst = Rutina.objects.filter(sexo=self.kwargs['pk2'], instructor=self.kwargs['pk'])
return inst
class SexoListView(ListView):
model = Sexo
对于测试我在sex_list模板中有以下内容
<a href="{% url 'rutina_list' pk=1 pk2=sexo.pk %}">
{{ sexo.genero }}
</a>
我只是pk与在instructor_list中选择的pk相同,但它显示了一个新的网页并刷新了pk。
我想要来自此网址的pk:
url(r'^instructor/(?P<pk>[\w-]+)$', views.SexoListView.as_view(), name='sexo_list'),
在此网址中接听并过去:
url(r'^instructor/(?P<pk>[\w-]+)/sexo/(?P<pk2>[\w-]+)$', views.RutinaListView.as_view(), name='rutina_list'),
有什么想法吗?
答案 0 :(得分:0)
对于你可以拥有的最后一个网址
request.META['HTTP_REFERER']
在您的视图中,这将为您提供最后一个网址。
我认为这不是你想要的,也许你可以给我们一些关于你想做什么的见解。