Django使用参数将httpresponedirect返回到一个url

时间:2023-01-13 19:54:15

I have a situation in my project where i need to make a redirection of the user to an url containing a parameter, (it is declared in the urls.py like:

在我的项目中,我需要将用户重定向到包含参数的url(它在url中声明)。py:

url(r'^notamember/(?P<classname>\w+)/$', 
                           notamember,
                           name='notamember'),)

How can i put that parameter in the return HttpResponseRedirect? I tried like: return HttpResponseRedirect('/classroom/notamember/classname'),anyway, this is foolish, i know, i cannot consider the 'classmane' as a parameter. For clarity, my view is:

如何将该参数放入返回HttpResponseRedirect?我尝试:返回HttpResponseRedirect('/classroom/notamember/classname'),无论如何,这是愚蠢的,我知道,我不能把'classmane'当作参数。为了清晰起见,我的观点是:

def leave_classroom(request,classname):
theclass = Classroom.objects.get(classname = classname)
u = Membership.objects.filter(classroom=theclass).get(member = request.user).delete()
return HttpResponseRedirect('/classroom/notamember/theclass/')

how can i include the variable 'theclass' in that url? Thanks a lot!

如何在url中包含变量“theclass”?谢谢!

3 个解决方案

#1


39  

Try this:

试试这个:

return HttpResponseRedirect('/classroom/notamember/%s/' % classname)

EDIT:

编辑:

This is surely better (Daniel Roseman's answer):

这当然更好(Daniel Roseman的回答):

from django.core.urlresolvers import reverse

url = reverse('notamember', kwargs={'classname': classname})
return HttpResponseRedirect(url)

#2


49  

This should not be complicated. The argument to HttpResponseRedirect is simply a string, so the normal rules for building up a string apply here. However, I don't think you want the theclass variable in there, as that is a ClassRoom object, not a string. You presumably want the classname instead. adamk has given you the right answer here.

这并不复杂。HttpResponseRedirect的参数只是一个字符串,因此构建一个字符串的常规规则在这里适用。但是,我不认为你想要在这里包含这个类变量,因为它是一个class对象,而不是一个string。您可能想要的是类名。亚当给了你正确的答案。

However, having said that you can just use a string, what you should actually do is use the reverse function. This is because you might later decide to change the URL structure, and rather than having to look through your code finding each place you've hard-coded the URL string, you should rely on having defined them in one single place: your urls.py file. So you should do something like this:

但是,说到可以使用字符串,实际上应该使用反向函数。这是因为您以后可能会决定更改URL结构,而不必在代码中查找硬编码URL字符串的每个位置,您应该依赖于在一个地方定义它们:URL。py文件。所以你应该这样做:

from django.core.urlresolvers import reverse

url = reverse('notamember', kwargs={'classname': classname})
return HttpResponseRedirect(url)

#3


3  

Actually, the shortcut redirect takes view names and model (which has get_absolute_url defined) names too.

实际上,这个快捷方式重定向将查看名称和模型(它有get_absolute_url定义的)名称。

from django.shortcuts import redirect

return redirect(leave_classroom)

#1


39  

Try this:

试试这个:

return HttpResponseRedirect('/classroom/notamember/%s/' % classname)

EDIT:

编辑:

This is surely better (Daniel Roseman's answer):

这当然更好(Daniel Roseman的回答):

from django.core.urlresolvers import reverse

url = reverse('notamember', kwargs={'classname': classname})
return HttpResponseRedirect(url)

#2


49  

This should not be complicated. The argument to HttpResponseRedirect is simply a string, so the normal rules for building up a string apply here. However, I don't think you want the theclass variable in there, as that is a ClassRoom object, not a string. You presumably want the classname instead. adamk has given you the right answer here.

这并不复杂。HttpResponseRedirect的参数只是一个字符串,因此构建一个字符串的常规规则在这里适用。但是,我不认为你想要在这里包含这个类变量,因为它是一个class对象,而不是一个string。您可能想要的是类名。亚当给了你正确的答案。

However, having said that you can just use a string, what you should actually do is use the reverse function. This is because you might later decide to change the URL structure, and rather than having to look through your code finding each place you've hard-coded the URL string, you should rely on having defined them in one single place: your urls.py file. So you should do something like this:

但是,说到可以使用字符串,实际上应该使用反向函数。这是因为您以后可能会决定更改URL结构,而不必在代码中查找硬编码URL字符串的每个位置,您应该依赖于在一个地方定义它们:URL。py文件。所以你应该这样做:

from django.core.urlresolvers import reverse

url = reverse('notamember', kwargs={'classname': classname})
return HttpResponseRedirect(url)

#3


3  

Actually, the shortcut redirect takes view names and model (which has get_absolute_url defined) names too.

实际上,这个快捷方式重定向将查看名称和模型(它有get_absolute_url定义的)名称。

from django.shortcuts import redirect

return redirect(leave_classroom)