Django:在自定义404错误处理程序中显示由Http404(“msg”)传递的错误消息(Django: Display error message passed by Http404(“msg”) in custom 404 error handler)
使用django 1.11,我通过写入我的主URLs.py来定义一个自定义404处理程序:
handler404 = 'myApp.views.myError'
函数
myError()
看起来像这样:def myError(request): #errorMsg=request.error_404_message return render(request,'404.html')
当我调用
raise Http404("My msg")
时,此功能有效。 但是,当我尝试通过Http404
调用传递消息时(请参阅myError中的注释部分),我的代码崩溃了:服务器错误(500)
那我怎么能得到错误信息? 我读了很多关于它的问题,但大多数答案都是指在html文件中获取消息而不是在自定义python错误处理函数中。
编辑:问题是,我看不到调试页面,因为错误显然只发生在调用myError()时才发生,只有在设置中
debug=False
时才会发生。Using django 1.11, I have defined a custom 404 handler, by writing in my main URLs.py:
handler404 = 'myApp.views.myError'
The function
myError()
looks like this:def myError(request): #errorMsg=request.error_404_message return render(request,'404.html')
This function works when I call
raise Http404("My msg")
. However, when I try to get the message passed by theHttp404
call (see commented section in myError), my code crashes:Server Error (500)
How can I then get the error message? I read a lot of questions about it like this one, but most answers refer to getting the message in the html file and not in a custom python error handler function.
EDIT: The problem is, that I cannot see the debug page, because the error obviously only occurs when myError() is called and this happens only, if
debug=False
in the settings.
原文:https://stackoverflow.com/questions/44570029
最满意答案
在您引用的行之前,对于名为“style”的目录,您需要:
RewriteRule ^style/ - [L]
连字符表示“无重定向”,“[L]”表示“最后一条规则”,因为不会继续尝试将URL与后续规则相匹配。 您可以根据需要添加尽可能多的这些行,但它们必须位于您在问题中提供的行之前。
Before the line you have quoted, for a directory named 'style' for instance, you need:
RewriteRule ^style/ - [L]
The hyphen means 'no redirection', and the '[L]' means 'last rule', as in don't carry on trying to match the URL to the follwing rules. You can put as many of these lines in as you like, but they must be before the line you give in the question.
相关问答
更多-
以下代码应该可以在任何地方将当前目录中的file.html重定向到当前目录本身,而无需任何路径硬编码。 它将REQUEST_URI变量(完整路径)与RewriteRule匹配的URI(相对于当前路径)进行比较,得到%{ENV:BASE}变量的差异。 RewriteEngine On # find rewrite base dynamically RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$ RewriteRule ^.*$ - [E=BASE:%2] Re ...
-
奇怪的重定向与url重写(strange redirect with url rewrite)[2022-06-19]
使用下面的.htaccess代码Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L ... -
您无法将网址重写为其他网域。 您可能需要进行重定向,或者您可以使用iframe在www.mydomain.com/mydirectory页面中包含subdomain.domain.com。 如果你想将subdomain.domain.com重写为类似domain.com/subdomain的东西,那么这是可能的,因为它是相同的域名,这是web.config中的规则:
... -
在你的root .htaccess使用它: RewriteEngine on RewriteCond %{HTTPS} on RewriteRule ^particularDirectory/? http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L] 或者在您的particularDirectory/.htaccess : RewriteEngine on RewriteCond %{HTTPS} on RewriteRule ^ http://%{HTTP_HOST ...
-
Redirect是mod_alias的一部分,而重写的东西是mod_rewrite的一部分。 这两个不同的模块都应用于同一个请求,从而导致它同时重定向和重写。 如果请求是/someoldpage.pjp并且没有重写到index.php,那么你想要做的是重定向,所以你需要在这里只使用mod_rewrite: # disable directory browsing Options All -Indexes # start rewrites RewriteEngine on RewriteRule ^som ...
-
使用IIRF重写/重定向URL(Rewrite/Redirect URL using IIRF)[2023-02-12]
由于您希望使用301重定向到新的(永久)URL,因此您应该使用RedirectRule 。 假设您有一个配置为响应subdomain.site.org和subdomain2.site.org ,您将需要一个RewriteCond ,以便仅重定向来自旧子域的请求。 RewriteCond %{SERVER_NAME} ^subdomain\.site\.org$ RedirectRule ^/dir1/dir2/dir3/page\.cfm\?pc=(\d+)$ http://subdomain2.site. ... -
好吧,我相信我现在已经解决了这个问题 - 事后的回答显而易见。 我在/ blog /目录下创建了另一个.htaccess文件,并在其中放置了相关规则 - 似乎完美地工作。 Well, I believe I've fixed this now - and the answer in hindsight is glaringly obvious. I've created another .htaccess file in the /blog/ directory and placed the relevan ...
-
这些是/research/.htaccess文件中需要的规则: RewriteEngine On RewriteBase /research/ RewriteCond %{THE_REQUEST} /index\.php\?quote=([^\s&]+) [NC] RewriteRule ^ %1? [R=301,L,NE] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/] ...
-
在您引用的行之前,对于名为“style”的目录,您需要: RewriteRule ^style/ - [L] 连字符表示“无重定向”,“[L]”表示“最后一条规则”,因为不会继续尝试将URL与后续规则相匹配。 您可以根据需要添加尽可能多的这些行,但它们必须位于您在问题中提供的行之前。 Before the line you have quoted, for a directory named 'style' for instance, you need: RewriteRule ^style/ ...
-
重定向url并重写它(Redirect url and rewrite it)[2022-05-03]
尝试: