我的Django /login网址在网站上显示为空的原因是什么?

huangapple go评论66阅读模式
英文:

Why is my django /login url shows noting on website

问题

以下是您提供的代码的翻译:

urls.py

from django.urls import path
from .views import *

urlpatterns = [
    path('', home, name='home'),
    path('<str:year>/', companies, name='company'),
    path('<str:year>/<str:company>', experience, name='experience'),
    path('login', loginUser, name='login'),
    path('logout', logoutUser, name='logout')
]

views.py

def loginUser(request):
    return render(request, 'App/login.html', context={})

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Placemet Freak - SFIT</title>
</head>
<body>
    <h1>Login</h1>
</body>
</html>

输出:
输出链接
它显示一个空白页面,所有其他URL都正常工作。

文件结构:
文件结构链接

它总是返回一个空白页面,应该返回<h1>Login</h1>

英文:

urls.py

from django.urls import path
from . views import *

urlpatterns=[
    path(&#39;&#39;,home,name=&#39;home&#39;),
    path(&#39;&lt;str:year&gt;/&#39;,companies,name=&#39;company&#39;),
    path(&#39;&lt;str:year&gt;/&lt;str:company&gt;&#39;,experience,name=&#39;experience&#39;),
    path(&#39;login&#39;,loginUser,name=&#39;login&#39;),
    path(&#39;logout&#39;,logoutUser,name=&#39;logout&#39;)
]

views.py

def loginUser(request):
    return render(request,&#39;App/login.html&#39;,context={})

login.html

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Placemet Freak - SFIT&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Login&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;

Output
Output
It shows a blank page, all other urls are working properly

File Structure
File Structure

It always returns a blank page, it should return <h1> Login </h1>

答案1

得分: 1

如果您写login/,它将匹配companies视图。实际上,'login'str路径转换器接受,因此该视图将被触发。

您可以将login/模式放在首位:

urlpatterns = [
    path('', home, name='home'),
    path('<b>login/</b>', loginUser, name='login'),
    path('logout/', logoutUser, name='logout'),
    path('<str:year>/', companies, name='company'),
    path('<str:year>/<str:company>', experience, name='experience'),
]

但可能您希望将year变得更具体,并且仅接受数字序列:

urlpatterns = [
    path('', home, name='home'),
    path('<int:year>/', companies, name='company'),
    path('<int:year>/<str:company>', experience, name='experience'),
    path('login/', loginUser, name='login'),
    path('logout/', logoutUser, name='logout'),
]
英文:

If you write login/ it will match the companies view. Indeed, &#39;login&#39; is accepted by the str path converter, and thus that view will fire.

You can put the login/ pattern first:

<pre><code>urlpatterns = [
path('', home, name='home'),
path('<b>login/</b>', loginUser, name='login'),
path('logout/', logoutUser, name='logout'),
path('&lt;str:year&gt;/', companies, name='company'),
path('&lt;str:year&gt;/&lt;str:company&gt;', experience, name='experience'),
]</code></pre>

but likely you want to make the year more specific, and only accept a sequence of digits:

<pre><code>urlpatterns = [
path('', home, name='home'),
path('&lt;<b>int:</b>year&gt;/', companies, name='company'),
path('&lt;<b>int:</b>year&gt;/&lt;str:company&gt;', experience, name='experience'),
path('login/', loginUser, name='login'),
path('logout/', logoutUser, name='logout'),
]</code></pre>

huangapple
  • 本文由 发表于 2023年7月20日 21:33:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定