django中如何通过url传递两个参数
How do you pass two parameters through url in django
我想在django中通过url传递两个参数。首先,这是我目前的 html:
{% for lesson in lessons %}
{% for class in lessons %}
<a href="{% url 'next-page' lesson.pk %}">Link to next page {{lesson}} {{class}}</a>
{% endfor %}
{% endfor %}
这是我的 url:
path('nextpage/<int:pk>/', ListView.as_view(), name='next-page'),
但是,我想要如下内容:
{% for lesson in lessons %}
{% for class in lessons %}
<a href="{% url 'next-page' lesson.pk class.pk %}">Link to next page {{lesson}} {{class}}</a>
{% endfor %}
{% endfor %}
我的 url 是:
path('nextpage/<int1:pk>/<int2:pk>', ListView.as_view(), name='next-page'),
然后我希望能够访问下面视图中的主键:
class LessonList(generic.ListView):
model = Lesson
template_name = 'lesson_list.html'
context_object_name = "lessons"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
lesson_id = self.kwargs['pk1']
class_id = self.kwargs['pk2']
return context
显然,上面的代码是行不通的。但是,我希望基本上能够在 url 中传递两个参数,并在我的视图 get_context_data
中访问它们。谢谢,请留下任何问题。
您在定义路径时出错,应该是 <int:pk<b>1</b>>
而不是 <int1:pk>
,所以:
path('nextpage/<strong><int:pk1></strong>/<strong><int:pk2></strong>/', ListView.as_view(), name='next-page'),
那么你确实可以使用:
<a href="{% url 'next-page' <strong>lesson.pk</strong> <strong>class.pk</strong> %}">Link to next page {{lesson}} {{class}}</a>
当然,lesson
和 class
都存在于上下文中。
我想在django中通过url传递两个参数。首先,这是我目前的 html:
{% for lesson in lessons %}
{% for class in lessons %}
<a href="{% url 'next-page' lesson.pk %}">Link to next page {{lesson}} {{class}}</a>
{% endfor %}
{% endfor %}
这是我的 url:
path('nextpage/<int:pk>/', ListView.as_view(), name='next-page'),
但是,我想要如下内容:
{% for lesson in lessons %}
{% for class in lessons %}
<a href="{% url 'next-page' lesson.pk class.pk %}">Link to next page {{lesson}} {{class}}</a>
{% endfor %}
{% endfor %}
我的 url 是:
path('nextpage/<int1:pk>/<int2:pk>', ListView.as_view(), name='next-page'),
然后我希望能够访问下面视图中的主键:
class LessonList(generic.ListView):
model = Lesson
template_name = 'lesson_list.html'
context_object_name = "lessons"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
lesson_id = self.kwargs['pk1']
class_id = self.kwargs['pk2']
return context
显然,上面的代码是行不通的。但是,我希望基本上能够在 url 中传递两个参数,并在我的视图 get_context_data
中访问它们。谢谢,请留下任何问题。
您在定义路径时出错,应该是 <int:pk<b>1</b>>
而不是 ,所以:<int1:pk>
path('nextpage/<strong><int:pk1></strong>/<strong><int:pk2></strong>/', ListView.as_view(), name='next-page'),
那么你确实可以使用:
<a href="{% url 'next-page' <strong>lesson.pk</strong> <strong>class.pk</strong> %}">Link to next page {{lesson}} {{class}}</a>
当然,lesson
和 class
都存在于上下文中。