英文:
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('',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>
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, 'login'
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('<str:year>/', companies, name='company'),
path('<str:year>/<str:company>', 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('<<b>int:</b>year>/', companies, name='company'),
path('<<b>int:</b>year>/<str:company>', experience, name='experience'),
path('login/', loginUser, name='login'),
path('logout/', logoutUser, name='logout'),
]</code></pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论