Django: 登录后页面的urlpatterns问题

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

Django: Problem with urlpatterns for a page after login

问题

登录后,我无法访问重定向页面(home.html)。页面未找到,因为我拼错了home页面的URL模式。我是Django的新手。

urlpatterns中,index页面、login页面和logout页面都能正常工作。但是home页面不能正常工作。我认为问题出在home的URL模式上。该页面位于templates/app1/home.html

Project/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('app1.urls'))
]

App/urls.py

from django.urls import path, include
from . import views
from app1.views import index
from app1 import views

urlpatterns = [
    path('', index, name='index'),
    path('login/', views.sign_in, name='login'),
    path('logout/', views.sign_out, name='logout'),

    path('home/', views, name='home'), #错误在这里???
]

views.py中,我没有任何与home相关的函数。我只有登录/注销函数正常工作。

如何正确显示home页面?

英文:

After login, i can't access the redirect page (home.html). The page is not found, because I misspelled the urlpatterns of the home page. I'm new to Django.

In urlpatterns work fine correctly: index page, login page and logout page. But the home page doesn't. I believe the problem is the urlpatterns of the home. The page is located at templates/app1/home.html

Project/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('app1.urls'))

App/urls.py

from django.urls import path, include
from . import views
from app1.views import index
from app1 import views

urlpatterns = [
    path('', index, name='index'),
    path('login/', views.sign_in, name='login'),
    path('logout/', views.sign_out, name='logout'),

    path('home/', views, name='home'), #ERROR HERE???
 ]

In views.py I don't have any home related functions. I only have the login/logout functions working correctly.

How can I display the home correctly?

答案1

得分: 1

以下是翻译好的部分:

"你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:

Well you need a view. A view is a function that turns a HTTP request (passed as a parameter) into a response (what you return in the function). So you can make a home view:

app1/views.py

from django.contrib.auth.decorators import login_required

@login_required
def home(request):
return render(request, 'app1/home.html')"

"你需要一个视图。视图是一个将HTTP请求(作为参数传递)转换为响应(在函数中返回的内容)的函数。因此,你可以创建一个home视图:

# app1/views.py

from django.contrib.auth.decorators import login_required

@login_required
def home(request):
    return render(request, 'app1/home.html')

"and make the template in app1/templates/app1/home.html. The @login_required decorator will ensure only logged in users can visit the view."

"然后在app1/templates/app1/home.html中创建模板。@login_required装饰器将确保只有已登录的用户可以访问该视图。"

"In the urls.py, you can link this to the home view:

from app1 import views
from django.urls import include, path

urlpatterns = [
    path('', views.index, name='index'),
    path('login/', views.sign_in, name='login'),
    path('logout/', views.sign_out, name='logout'),
    path('home/', views.home, name='home'),
]

urls.py中,你可以将这个链接到home视图:

from app1 import views
from django.urls import include, path

urlpatterns = [
    path('', views.index, name='index'),
    path('login/', views.sign_in, name='login'),
    path('logout/', views.sign_out, name='logout'),
    path('home/', views.home, name='home'),
]

"I already have the login and logout functions (copied from a tutorial) which redirect to home."

"我已经有了登录和注销函数(从教程中复制而来),它们会重定向到主页。"

"Please don't copy. Following templates makes a lot of sense, just copying what tutorials say however is likely one of the worst ways to get used to a framework. It takes a lot of skill from the tutor to explains how something works, but it is often worth it. You probably do not need to write views to log in or log out anyway. Indeed, Django has builtin views to login and logout. You can work with:

from app1 import views
from django.contrib.auth.views import LoginView, LogoutView
from django.urls import include, path, reverse_lazy

urlpatterns = [
    path('', views.index, name='index'),
    path(
        'login/', LoginView.as_view(next_page=reverse_lazy('home')), name='login'
    ),
    path('logout/', LogoutView.as_view(), name='logout'),
    path('home/', views.home, name='home'),
]

"请不要复制。遵循模板是有意义的,但仅仅复制教程中的内容通常是熟悉框架的最糟糕的方式之一。教程作者要解释如何使某些东西工作需要很多技能,但通常是值得的。实际上,你可能根本不需要编写用于登录或注销的视图。事实上,Django内置了用于登录和注销的视图。你可以使用以下方式:

from app1 import views
from django.contrib.auth.views import LoginView, LogoutView
from django.urls import include, path, reverse_lazy

urlpatterns = [
    path('', views.index, name='index'),
    path(
        'login/', LoginView.as_view(next_page=reverse_lazy('home')), name='login'
    ),
    path('logout/', LogoutView.as_view(), name='logout'),
    path('home/', views.home, name='home'),
]

"you can further customize these with a custom template, etc. In very rare cases, you might want to implement a custom login view, but that is for most vanilla web applications, not necessary at all. A web framework normally aims to let programmers develop an application as fast as possible, so having to copy a lot of code for standard use cases, makes not a lot of sense."

"你还可以进一步使用自定义模板等进行自定义。在非常罕见的情况下,你可能想要实现自定义登录视图,但这仅适用于大多数标准的Web应用程序,完全不必要。Web框架通常旨在让程序员尽快开发应用程序,因此为标准用例复制大量代码没有多大意义。"

英文:

Well you need a view. A view is a function that turns a HTTP request (passed as a parameter) into a response (what you return in the function). So you can make a home view:

<pre><code># app1/views.py

from django.contrib.auth.decorators import login_required

<b>@login_required</b>
def home(request):
return render(request, <b>'app1/home.html'</b>)</code></pre>

and make the template in app1/templates/app1/home.html. The @login_required decorator&nbsp;<sup>[Django-doc]</sup> will ensure only logged in users can visit the view.

In the urls.py, you can link this to the home view:

<pre><code>from app1 import views
from django.urls import include, path

urlpatterns = [
path('', views.index, name='index'),
path('login/', views.sign_in, name='login'),
path('logout/', views.sign_out, name='logout'),
path('home/', <b>views.home</b>, name='home'),
]</code></pre>

> I already have the login and logout functions (copied from a tutorial) which redirect to home.

Please don't copy. Following templates makes a lot of sense, just copying what tutorials say however is likely one of the worst ways to get used to a framework. It takes a lot of skill from the tutor to explains how something works, but it is often worth it. You probably do not need to write views to log in or log out anyway. Indeed, Django has builtin views to login and logout. You can work with:

<pre><code>from app1 import views
from django.contrib.auth.views import LoginView, LogoutView
from django.urls import include, path, reverse_lazy

urlpatterns = [
path('', views.index, name='index'),
path(
'login/', <b>LoginView.as_view(next_page=reverse_lazy('home'))</b>, name='login'
),
path('logout/', LogoutView.as_view(), name='logout'),
path('home/', views.home, name='home'),
]</code></pre>

you can further customize these with a custom template, etc. In very rare cases, you might want to implement a custom login view, but that is for most vanilla web applications, not necessary at all. A web framework normally aims to let programmers develop an application as fast as possible, so having to copy a lot of code for standard use cases, makes not a lot of sense.

huangapple
  • 本文由 发表于 2023年6月22日 04:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526767.html
匿名

发表评论

匿名网友

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

确定