简体   繁体   English

在Django 1.9中使用网址

[英]Using urls in Django 1.9

I'm using Django 1.9.7 with Python 3.5.1 我正在将Django 1.9.7与Python 3.5.1结合使用

I'm rather new to Django, building a simple application. 我对Django很陌生,构建了一个简单的应用程序。 I currently have an issue with URLs. 我目前对网址有疑问。

I would like to have the following behaviour: if user gets to http:.../entry/2016/03 he sees data for March 2016 but if user gets t: http:.../entry then rather than getting a 404, he should be redirected to http:.../entry/2016/06 (assuming we're in June 2016). 我希望具有以下行为:如果用户访问http:... / entry / 2016/03,他会看到2016年3月的数据,但是如果用户访问t:http:... / entry,则不是得到404,他应该重定向到http:... / entry / 2016/06(假设我们在2016年6月)。

Currently, here's how I did it: 目前,这是我的操作方法:

url(r'^entry/$', Entry.as_view(), name='entry0'),
url(r'^entry/(?P<year>\d{4})/(?P<month>\d{1,2})/$', Entry.as_view(), name='entry'),

and the view is defined as: 该视图定义为:

def get(self, request, year=datetime.datetime.year, month=datetime.datetime.month):

in the template, I have: 在模板中,我有:

<li><a href="{% url 'entry' year month%}">Entry</a></li> 

The issue is that when I go to /entry, I get the following error: 问题是当我进入/ entry时,出现以下错误:

NoReverseMatch at /entry/

Reverse for 'entry' with arguments '(<attribute 'year' of 'datetime.date' objects>, <attribute 'month' of 'datetime.date' objects>)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['entry/(?P<year>\\d{4})/(?P<month>\\d{1,2})/$']

However, getting to /entry/2016/06 works properly. 但是,进入/ entry / 2016/06可以正常工作。

What would be the cleanest way to get this working? 使此工作最干净的方法是什么? Can I achieve this with a single url and view? 我可以使用单个网址和视图来实现吗?

Best regards 最好的祝福

Jean-Noël 让·诺埃尔

#

As suggested, I modified a bit the view: 如建议的那样,我对视图进行了一些修改:

url(r'^entry/$', Entry.as_view(), name='entry0'),
url(r'^entry/(?P<year>\d{4})/(?P<month>\d{1,2})/$', Entry.as_view(), name='entry'),

the urls remain the same: 网址保持不变:

<li><a href="{% url 'entry0' %}">Entry</a></li> 

the template only points to the empty url: 模板仅指向空网址:

 <li><a href="{% url 'entry0' %}">Entry</a></li> 

This seems to work; 这似乎可行; when clicking on the link in the template, I'm sent to the default values; 单击模板中的链接时,我会收到默认值; but if I specify a value in the url, I'm sent to the right values. 但是,如果我在网址中指定一个值,则会发送给正确的值。

My last question is: can't I do that in one single view and url? 我的最后一个问题是:我不能在单个视图和URL中做到这一点吗? Basically, when using /entry, the parameters would be set to None, while when specifying them, they're received in the view. 基本上,使用/ entry时,参数将设置为“无”,而指定它们时,它们将在视图中接收。

THanks a lot 非常感谢

Jean-Noël 让·诺埃尔

You're passing attributes that do not have any value as your defaults. 您传递的属性没有默认值。 You should do this instead: 您应该这样做:

from datetime import datetime as dt

def get(self, request, year=None, month=None):
    if year is None:
        year = dt.today().year
    if month is None:
        month = dt.today().month

To use this in your template: 要在模板中使用它,请执行以下操作:

<li><a href="{% url 'entry0' %}">Entry</a></li> 

您的url模板标记应获取关键字args而不是args

<li><a href="{% url 'entry' year=year month=month%}">Entry</a></li> 

OK, first things first. 好,首先 The whole logic of yours isn't clean or pythonic. 您的整个逻辑不是干净的或Pythonic的。 You need 1 url, 1 view and 1 template 您需要1个网址,1个视图和1个模板

url(r'^entry/(?P<year>\d{4})/(?P<month>\d{1,2})/$', Entry.as_view(),name='entry')

define your view 定义你的看法

def get(self, request, year=None, month=None):

Now, inside your view check if your params have value None, 现在,在视图内部检查参数是否具有值None,

if (year is None) and (month is None) :
# set today values

mplah mplah mplah..... hope you get the point.... mplah mplah mplah .....希望您能明白这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM