K
K
Konstantin Malyarov2018-02-14 15:50:12
Django
Konstantin Malyarov, 2018-02-14 15:50:12

I specify one view for url, and fulfills another?

urls.py

from django.conf.urls import url
from staff import views

urlpatterns = [
    url(r'^$', views.index, name='staff'),
    url(r'(?P<username>[-\w]+)/$', views.user, name='user'),
    url(r'(?P<username>[-\w]+)/logout/$', views.logout, name='user_logout'),
]

views.py
def user(request, username):
    print('user')
    return render(request, 'staff/index.html', locals())


def logout(request, username):
    print('test')
    auth.logout(request)
    username = None
    print('test')
    return redirect('staff/index.html', locals())

index.html
<a href="{% url 'user_logout' user %}" class="btn btn-sm btn-danger ion-log-out">Выход</a>

When clicking on the link, it works view.index, not view.loguot.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Animkim, 2018-02-14
@Konstantin18ko

The regex for views.user is written so that your url for views.logout fits it,
you can visually see it here https://regex101.com/
Please mark it as a solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question