Django:获取以前的URL [英] Django: getting previous url
问题描述
我有一个Post模型,在添加到数据库之前需要一定的类别,我希望自动生成该类别。点击addPost按钮可以转到不同的页面,所以该类别将通过获取前一页面的一部分来确定。
I have a Post model that requires a certain category before being added to the database, and I want the category to be generated automatically. Clicking the addPost button takes you to a different page and so the category will be determined by taking a part of the previous page url.
有没有办法获取以前的页面网址为字符串?
Is there a way to get the previous page url as a string?
谢谢
编辑:我已经添加了我的AddPost按钮。
I have added my AddPost button here.
<aside class="addPost">
<article>
<form action="/Forum/addPost">
<input type="submit" name="submit" value="Add Post"/>
</form>
</article>
</aside>
推荐答案
您可以使用 self.request.META ['HTTP_REFERER']
,但如果只有您的标签上一页来自您的网站,则会存在,否则将不会有 HTTP_REFERER
在META dict
。所以要小心,请确保您正在使用 .get()
符号。
You can do that by using self.request.META['HTTP_REFERER']
, but it will exist if only your tab previous page was from your website, else there will be no HTTP_REFERER
in META dict
. So be careful and make sure that you are using .get()
notation instead.
# Returns None if user came from another website
referer = self.request.META.get('HTTP_REFERER')
这篇关于Django:获取以前的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!