Reputation: 608
I feel like this is going to be an embarrassing question but I can't find the answer I've tried google but I am getting fairly frustrated so I just decided to ask here. I've got this code snippet inside a template file in django
{% url cal.views.month year month "next" %}
I run the code and get this error Reverse for 'courseCalendar.views.month' with arguments '(2013, 9, u'next')' not found
Why is it that when I try to pass variables in a string it puts the u in there as well. This is very frustrating and I would really appreciate someones two cents.
Edit: Everything works fine if I don't include the string "next" and use a variable instead like the year and month variables so it's not a url problem or template problem.
url pattern
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'', 'courseCalendar.views.main'),
url(r'^month/(?P<year>\d{4})/(?P<month>\d{2})/(?P<change>[a-z])/$', 'courseCalendar.views.month', name="month"),
)
Views
# Create your views here.
import time
import calendar
from datetime import date, datetime, timedelta
#from django.contrib.auth.decorators import login_required For login requirements.
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import get_object_or_404, render_to_response
from courseCalendar.models import *
monthNames = "Jan Feb Mar Apr May Jun Jly Aug Sep Oct Nov Dec"
monthNames = monthNames.split()
#@login_required This should be uncommented in the future but for now we don't want to deal with it. Default set to 2013 for now.
def main(request, year="None"):
if year == "None":
year = time.localtime()[0]
else:
year = int(year)
currentYear,currentMonth = time.localtime()[:2]
totalList = []
for y in [year]:
monthList = []
for n, month in enumerate(monthNames):
course = current = False
courses = Course.objects.filter(courseDate__year=y, courseDate__month=n+1)
if courses:
course = True
if y==currentYear and n+1 == currentMonth:
current = True
monthList.append(dict(n=n+1, name=month, course=course, current=current))
totalList.append((y,monthList))
#return render_to_response("courseCalendar/Templates/main.html", dict(years=totalList, user=request.user, year=year, reminders=reminders(request))) <-- Later in the build
return render_to_response("main.html", dict(years=totalList, user=request.user, year=year))
def month(request, year=1, month=1, change="None"):
if year == "None":
year = time.localtime()[0]
else:
year = int(year)
if month == "None":
month = time.localtime()[1]
else:
month = int(month)
if change in ("next", "prev"):
todaysDate, mdelta = date(year, month, 15), timedelta(days=31)
if change == "next":
mod = mdelta
elif change == "prev":
mod = -mdelta
year, month = (todaysDate+mod).timertuple()[:2]
cal = calendar.Calendar()
month_days = cal.itermonthdays(year, month)
nyear, nmonth, nday = time.localtime()[:3]
lst = [[]]
week = 0
for day in month_days:
courses = current = False
if day:
courses = Course.objects.filter(courseDate__year=year, courseDate__month=month, courseDate__day=day)
if day == nday and year == nyear and month == nmonth:
current = True
lst[week].append((day, courses, current))
if len(lst[week]) == 7:
lst.append([])
week += 1
return render_to_response("month.html", dict(year=year, month=month, user=request.user, month_days=lst, monthNames=monthNames[month-1]))
main.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Need to figure out how to pass the year variable properly in the url arguements. The way it was originally written didn't work and adding quotes appended a "u" to the areguements-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Year View</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
{% load staticfiles %}
<link rel="stylesheet" href="{% static "courseCalendar/css/layout.css" %}" type="text/css" />
</head>
<body>
<div id="wrapper">
<a href="{% url "courseCalendar.views.main" %}"><< Prev</a>
<a href="{% url "courseCalendar.views.main" %}">Next >></a>
{% for year, months in years %}
<div class="clear"></div>
<h4>{{ year }}</h4>
{% for month in months %}
<div class=
{% if month.current %}"month current"{% endif %}
{% if not month.current %}"month"{% endif %}
>
{% if month.entry %}<b>{% endif %}
<a href="{% url "courseCalendar.views.month" year month "next"">{{ month.name }}</a>
{% if month.entry %}</b>{% endif %}
</div>
{% endfor %}
{% endfor %}
</div>
</body>
</html>
month.html
<a href="{% url "courseCalendar.views.month" %}"><< Prev</a>
<a href="{% url "courseCalendar.views.month" %}">Next >></a>
<h4>{{ month }} {{ year }}</h4>
<div class="month">
<table>
<tr>
<td class="empty">Mon</td>
<td class="empty">Tue</td>
<td class="empty">Wed</td>
<td class="empty">Thu</td>
<td class="empty">Fri</td>
<td class="empty">Sat</td>
<td class="empty">Sun</td>
</tr>
{% for week in month_days %}
<tr>
{% for day, courses, current in week %}
<td class= {% if day == 0 %}"empty"{% endif %}
{% if day != 0 and not current %}"day"{% endif %}
{% if day != 0 and current %}"current"{% endif %}
{% if day != 0 %}"Add day click code here"{% endif %} >
{% if day != 0 %}
{{ day }}
{% for course in courses %}
<br />
<b>{{ course.courseCreator }}</b>
{% endfor %}
{% endif %}
{% endfor %}
</td>
{% endfor %}
</table>
<div class="clear"></div>
</div>
Upvotes: 0
Views: 5719
Reputation:
Change to the following:
In your view file:
def month(request, year=1, month=1, change="None"):
In your url file:
url(r'^month/(?P<year>\d{4})/(?P<month>\d{2})/(?P<change>\w+)/$', 'courseCalendar.views.month', name="month"),
After you change to the above, post here the error message if you're receiving an error.
Upvotes: 1