Python ValueError:Don';t在调用reverse()时混合*args和**kwargs!
我正在尝试呈现_-to-u响应,以将视图的呈现响应返回到ajax调用,但我遇到了以下错误,我真的不明白Python ValueError:Don';t在调用reverse()时混合*args和**kwargs!,python,django,django-views,Python,Django,Django Views,我正在尝试呈现_-to-u响应,以将视图的呈现响应返回到ajax调用,但我遇到了以下错误,我真的不明白 Internal Server Error: /schedules/calendar/2014/10/1/ Traceback (most recent call last): File "/blahblahblah/django/core/handlers/base.py", line 111, in get_response response = wrapped_callbac
Internal Server Error: /schedules/calendar/2014/10/1/
Traceback (most recent call last):
File "/blahblahblah/django/core/handlers/base.py", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/blahblahblah/schedules/views.py", line 229, in month_view
return render_to_string(template, data)
File "/blahblahblah/django/template/loader.py", line 172, in render_to_string
return t.render(Context(dictionary))
File "/blahblahblah/django/template/base.py", line 148, in render
return self._render(context)
File "/blahblahblah/django/template/base.py", line 142, in _render
return self.nodelist.render(context)
File "/blahblahblah/django/template/base.py", line 844, in render
bit = self.render_node(node, context)
File "/blahblahblah/django/template/debug.py", line 80, in render_node
return node.render(context)
File "/blahblahblah/django/template/defaulttags.py", line 444, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/blahblahblah/django/core/urlresolvers.py", line 546, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/blahblahblah/django/core/urlresolvers.py", line 405, in _reverse_with_prefix
raise ValueError("Don't mix *args and **kwargs in call to reverse()!")
ValueError: Don't mix *args and **kwargs in call to reverse()!
我不明白这个错误是从哪里来的。根据django文档中的指示,我正在调用请求到响应。以下是我的看法:
def month_view(
request,
year,
month,
template='monthly_view.html',
user_id=None,
queryset=None
):
year, month = int(year), int(month)
cal = calendar.monthcalendar(year, month)
dtstart = datetime(year, month, 1)
last_day = max(cal[-1])
dtend = datetime(year, month, last_day)
if user_id:
profile = get_object_or_404(UserProfile, pk=user_id)
queryset = profile.occurrence_set
queryset = queryset._clone() if queryset else Occurrence.objects.select_related()
occurrences = queryset.filter(start_time__year=year, start_time__month=month)
def start_day(o):
return o.start_time.day
by_day = dict([(dt, list(o)) for dt,o in itertools.groupby(occurrences, start_day)])
data = {
'today': datetime.now(),
'calendar': [[(d, by_day.get(d, [])) for d in row] for row in cal],
'this_month': dtstart,
'next_month': dtstart + timedelta(days=+last_day),
'last_month': dtstart + timedelta(days=-1),
}
if request.is_ajax():
my_html = render_to_string('monthly_view.html', data)
return HttpResponse(json.dumps(my_html), content_type="application/json")
else:
raise Http404
以前有没有人遇到过这样的错误,或者知道它是从哪里来的?
在调用reverse之前的线路上,reverse的参数为
视图名称:时间表:月视图参数:[2014,9]kwargs:
{'user_pk':
''}
我尝试渲染的模板是:
<h4>
<a href="{% url 'schedules:monthly-view' last_month.year last_month.month user_pk=object.profile.pk %}"
title="Last Month">←</a>
{{ this_month|date:"F" }}
<a title="View {{ this_month.year}}" href='#'>
{{ this_month|date:"Y" }}</a>
<a href="{% url 'schedules:monthly-view' next_month.year next_month.month user_pk=object.profile.pk %}"
title="Next Month">→</a>
</h4>
<table class="month-view">
<thead>
<tr>
<th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th>
</tr>
</thead>
<tbody>
{% for row in calendar %}
<tr>
{% for day,items in row %}
<td{% ifequal day today.day %} class="today"{% endifequal %}>
{% if day %}
<div class="day-ordinal">
<a href="{% url 'schedules:daily-view' this_month.year this_month.month day user_pk=object.profile.pk %}">{{ day }}</a>
</div>
{% if items %}
<ul>
{% for item in items %}
<li>
<a href="{{ item.get_absolute_url }}">
<span class="event-times">{{ item.start_time|time }}</span>
{{ item.title }}</a>
</li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{{本月}日期:“F”}
森蒙特韦德苏弗里萨
{日历%中的行为%1}
{%为天,第%]行中的项目}
{%if day%}
{%if items%}
{items%%中的项的%s}
-
{%endfor%}
{%endif%}
{%endif%}
{%endfor%}
{%endfor%}
而url.py是
from django.conf.urls import patterns, url
from .views import (
CreateSessionView, CreateListingsView, SessionsListView,
month_view, day_view, today_view
)
urlpatterns = patterns('',
url(
r'^(?:calendar/)?$',
today_view,
name='today'
),
url(
r'^calendar/(?P<year>\d{4})/(?P<month>0?[1-9]|1[012])/(?P<user_pk>\d+)/$',
month_view,
name='monthly-view'
),
url(
r'^calendar/(?P<year>\d{4})/(?P<month>0?[1-9]|1[012])/(?P<day>[0-3]?\d)/(?P<user_pk>\d+)/$',
day_view,
name='daily-view'
),
来自django.conf.url导入模式,url
从。视图导入(
CreateSessionView、CreateSistingView、SessionListView、,
月视图、日视图、今天视图
)
urlpatterns=模式(“”,
网址(
r'^(?:日历/)?$,
今天,,
name='today'
),
网址(
r'^calendar/(?P\d{4})/(?P0?[1-9]| 1[012])/(?P\d+/$),
月视图,
name='monthly-view'
),
网址(
r'^calendar/(?P\d{4})/(?P0?[1-9]| 1[012])/(?P[0-3]?\d)/(?P\d+/$),
天景,
name='daily-view'
),
此外,以下是ajax请求:
<script>
function update_calendar(url) {
console.log("Calling url " + url)
$.ajax({
type: 'GET',
dataType: 'json',
url: url,
success: function(data, status, xhr) {
console.log('success')
console.log(data);
$('#schedule').html(data)
},
error: function() {
console.log('Something went wrong');
}
});
}
$(document).ready(function() {
// Initially set up calendar for current month
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth()+1;
var user_pk = {{ object.profile.pk }};
var url = '../schedules/calendar/'+year+'/'+month+'/'+user_id+'/';
// EDIT JUST ADDED THIS LINE HERE AND AM GETTING THE ERROR HERE NOW
url = "{% url 'schedules:monthly-view' year month user_pk=user_pk %}"
update_calendar(url);
$('#schedule > a').click(function(event) {
var url = $(this).attr('href');
update_calendar(url);
});
});
</script>
功能更新\日历(url){
log(“调用url”+url)
$.ajax({
键入:“GET”,
数据类型:“json”,
url:url,
成功:功能(数据、状态、xhr){
console.log('success')
控制台日志(数据);
$('#schedule').html(数据)
},
错误:函数(){
log(“出了点问题”);
}
});
}
$(文档).ready(函数(){
//初始设置当前月份的日历
var today=新日期();
var year=today.getFullYear();
var month=today.getMonth()+1;
var user_pk={{object.profile.pk};
var url='../schedules/calendar/'+year+'/'+month+'/'+user_id+'/';
//编辑刚刚在这里添加了这一行,我现在在这里得到了错误
url=“{%url”计划:月视图“年-月用户\u pk=user\u pk%}”
更新日历(url);
$(“#计划>a”)。单击(函数(事件){
var url=$(this.attr('href');
更新日历(url);
});
});
错误告诉您的确切信息:
使用以下任一关键字参数:
{% url 'schedules:monthly-view' year=last_month.year month=last_month.month user_pk=object.profile.pk %}
{% url 'schedules:monthly-view' last_month.year last_month.month object.profile.pk %}
或位置参数:
{% url 'schedules:monthly-view' year=last_month.year month=last_month.month user_pk=object.profile.pk %}
{% url 'schedules:monthly-view' last_month.year last_month.month object.profile.pk %}
这发生在HTML中的模板标记中。能否显示
{%url%}
标记在该模板中的任何用法?