简体   繁体   中英

Using urls in Django 1.9

I'm using Django 1.9.7 with Python 3.5.1

I'm rather new to Django, building a simple application. 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).

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:

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.

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? Basically, when using /entry, the parameters would be set to None, while when specifying them, they're received in the view.

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. You need 1 url, 1 view and 1 template

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,

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

mplah mplah mplah..... hope you get the point....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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