Django redirect to root from a view

后端 未结 2 1426
广开言路
广开言路 2021-01-04 11:49

I am creating a django project. However, I have come across a small hiccup. My urls.py looks like this

url(r\'^login/(?P)$\', \'Home.views.log         


        
相关标签:
2条回答
  • 2021-01-04 12:45

    If you look at the documentation for redirect, there are several things you can pass to the function:

    • A model
    • A view name
    • A URL

    In general, I think it's better to redirect to a view name rather than a URL. In your case, assuming your urls.py has an entry that looks something like:

    url(r'^$', 'Home.views.index'),
    

    I would instead use redirect like this:

    redirect('Home.views.index')
    
    0 讨论(0)
  • 2021-01-04 12:45

    I am using Django 3.1. This is what I do to achieve this:

    in urls.py

    from django.shortcuts import redirect
    
    urlpatterns = [
        path('', lambda req: redirect('/myapp/')),
        path('admin/', admin.site.urls),
        path('myapp/', include('myapp.urls'))
    ]
    
    0 讨论(0)
提交回复
热议问题