Django 视图和模板链接
Django views and templates links
我在 Django 中的 index.html 模板具有以下内容:
<ul>
<li><a href="{% url 'quantities' %} value="1">All</a></li>
<li><a href="{% url 'quantities' %} value="2">Subset1</a></li>
<li><a href="{% url 'quantities' %} value="3">Subset2</a></li>
<li><a href="{% url 'quantities' %} value="4">Subset3</a></li>
</ul>
因此 link 使用相同的模板,但是(取决于单击的 link)我想在我的 app/views.py:
中编写 if 函数
def quantities(request):
if value == "1":
levels = Model_1.objects.all()
elif value == "2":
levels = Model_1.objects.filter(createria=2)
elif value == "3":
levels = Model_1.objects.filter(createria=3)
elif value == "4":
levels = Model_1.objects.filter(createria=4)
能否获取上一页点击的link的值?
将值作为 GET 参数传递:
<ul>
<li><a href="{% url 'quantities' %}?value=1">All</a></li>
<li><a href="{% url 'quantities' %}?value=2">Subset1</a></li>
<li><a href="{% url 'quantities' %}?value=3">Subset2</a></li>
<li><a href="{% url 'quantities' %}?value=4">Subset3</a></li>
</ul>
使其可见:
def quantities(request):
value = request.GET.get('value', '1')
if value == "1":
levels = Model_1.objects.all()
elif value == "2":
levels = Model_1.objects.filter(createria=2)
elif value == "3":
levels = Model_1.objects.filter(createria=3)
elif value == "4":
levels = Model_1.objects.filter(createria=4)
我最好在 url 中传递值而不是 url 参数:
<ul>
<li><a href="{% url 'quantities_all' %}>All</a></li>
<li><a href="{% url 'quantities' '2' %}>Subset1</a></li>
<li><a href="{% url 'quantities' '3' %}>Subset2</a></li>
<li><a href="{% url 'quantities' '4' %}>Subset3</a></li>
</ul>
您的视图将如下所示:
def quantities(request, value=None):
if value in ('2', '3', '4'):
levels = Model_1.objects.filter(createria=int(value))
else:
levels = Model_1.objects.all()
...
别忘了将 url 更改为:
url(r'^quantities/$', 'app.views.quantities', name='quantities_all'),
url(r'^quantities/(\d+)/$', 'app.views.quantities', name='quantities'),
您需要在 url 配置中捕获数量。
https://docs.djangoproject.com/en/1.7/topics/http/urls/
这比传递 GET 参数更好,因为你有一个 link,link 这样 http://example.org/quantities/4/
看起来更好,在 Django 中这是一个约定:
<ul>
<li><a href="{% url 'quantities' 1 %}">All</a></li>
<li><a href="{% url 'quantities' 2 %}">Subset1</a></li>
<li><a href="{% url 'quantities' 3 %}">Subset2</a></li>
<li><a href="{% url 'quantities' 4 %}">Subset3</a></li>
</ul>
所以你的 urls.py 可能看起来像:
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^quantities/(\d+)/$', views.quantities), )
views.py 来自另一个答案
def quantities(request, value):
if value in ('2', '3', '4'):
levels = Model_1.objects.filter(createria=int(value))
else:
levels = Model_1.objects.all()
...
我在 Django 中的 index.html 模板具有以下内容:
<ul>
<li><a href="{% url 'quantities' %} value="1">All</a></li>
<li><a href="{% url 'quantities' %} value="2">Subset1</a></li>
<li><a href="{% url 'quantities' %} value="3">Subset2</a></li>
<li><a href="{% url 'quantities' %} value="4">Subset3</a></li>
</ul>
因此 link 使用相同的模板,但是(取决于单击的 link)我想在我的 app/views.py:
中编写 if 函数def quantities(request):
if value == "1":
levels = Model_1.objects.all()
elif value == "2":
levels = Model_1.objects.filter(createria=2)
elif value == "3":
levels = Model_1.objects.filter(createria=3)
elif value == "4":
levels = Model_1.objects.filter(createria=4)
能否获取上一页点击的link的值?
将值作为 GET 参数传递:
<ul>
<li><a href="{% url 'quantities' %}?value=1">All</a></li>
<li><a href="{% url 'quantities' %}?value=2">Subset1</a></li>
<li><a href="{% url 'quantities' %}?value=3">Subset2</a></li>
<li><a href="{% url 'quantities' %}?value=4">Subset3</a></li>
</ul>
使其可见:
def quantities(request):
value = request.GET.get('value', '1')
if value == "1":
levels = Model_1.objects.all()
elif value == "2":
levels = Model_1.objects.filter(createria=2)
elif value == "3":
levels = Model_1.objects.filter(createria=3)
elif value == "4":
levels = Model_1.objects.filter(createria=4)
我最好在 url 中传递值而不是 url 参数:
<ul>
<li><a href="{% url 'quantities_all' %}>All</a></li>
<li><a href="{% url 'quantities' '2' %}>Subset1</a></li>
<li><a href="{% url 'quantities' '3' %}>Subset2</a></li>
<li><a href="{% url 'quantities' '4' %}>Subset3</a></li>
</ul>
您的视图将如下所示:
def quantities(request, value=None):
if value in ('2', '3', '4'):
levels = Model_1.objects.filter(createria=int(value))
else:
levels = Model_1.objects.all()
...
别忘了将 url 更改为:
url(r'^quantities/$', 'app.views.quantities', name='quantities_all'),
url(r'^quantities/(\d+)/$', 'app.views.quantities', name='quantities'),
您需要在 url 配置中捕获数量。 https://docs.djangoproject.com/en/1.7/topics/http/urls/
这比传递 GET 参数更好,因为你有一个 link,link 这样 http://example.org/quantities/4/
看起来更好,在 Django 中这是一个约定:
<ul>
<li><a href="{% url 'quantities' 1 %}">All</a></li>
<li><a href="{% url 'quantities' 2 %}">Subset1</a></li>
<li><a href="{% url 'quantities' 3 %}">Subset2</a></li>
<li><a href="{% url 'quantities' 4 %}">Subset3</a></li>
</ul>
所以你的 urls.py 可能看起来像:
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^quantities/(\d+)/$', views.quantities), )
views.py 来自另一个答案
def quantities(request, value):
if value in ('2', '3', '4'):
levels = Model_1.objects.filter(createria=int(value))
else:
levels = Model_1.objects.all()
...