美好的一天,我有以下幾點看法:@login_required導致問題
class BookList(RequireLoginMixin, generic.ListView):
model = Book
template_name = 'book/list.html'
和
@login_required(login_url='/login/')
def fetch_book_author(request):
context = {"context": Book.objects.all()}
return render(request, 'book/pages/authors.html', context)
class RequireLoginMixin(object):
@classmethod
def as_view(cls, **initkwargs):
view = super(RequireLoginMixin, cls).as_view(**initkwargs)
return login_required(view)
我想拒絕,除了主頁我的所有網頁的訪問。任何網頁,我試着去訪問應用裝飾或混入後,是給我下面的錯誤:
^__debug__/
^admin/
^mwf/
The current URL, accounts/login/, didn't match any of these.
這裏是我的網址配置:
app_name = 'bookApp'
urlpatterns = [
url(r'^$', HomeView.as_view(), name='home'),
url(r'^about/$', AboutView.as_view(), name='about'),
url(r'^welcome/$', WelcomeView.as_view(), name='welcome'),
url(r'^fetch-books/$', fetch_book_author, name='fetch'),
url(r'^books/$', BookList.as_view(), name='books'),
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', kwargs={'next_page': '/mwf/'}),
url(r'^signup/$', UserRegistrationView.as_view(), name='signup'),
url('^', include('django.contrib.auth.urls')),
]
,並在我的設置,我有:
LOGIN_REDIRECT_URL = 'login/'
不知道這裏有什麼問題。 真的會感謝所有幫助
它看起來像你試圖使用的登錄URL是'accounts/login /',但沒有任何與urlpatterns中的URL匹配。嘗試將您的登錄模式更改爲'r ^'accounts/login/$'' – dkhaupt
並使用'LOGIN_REDIRECT_URL = reverse_lazy('login')'來避免硬編碼的網址。 – Risadinha
您是否已經導入django.contrib.auth.views?,還嘗試編寫'login'而不是'login /' – jsanchezs