我正在开发一个推文应用程序,虽然我可以在身份验证过程中登录,但我的注销功能不起作用。我将我的源代码留给解释。我等待你的帮助
我得到的错误如下: (http://127.0.0.1:8000/logout/?next=/) 我得到此网站无法访问错误! 更准确地说,我无法访问我想要的页面。当我点击注销时,我必须退出该帐户。 我还应该提到我正在使用 Python 3.12.0 和 Django。
我的网址.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("",include('tweetapp.urls')),
path("",include('django.contrib.auth.urls')),]
我的观点.py
from django.shortcuts import render, redirect
from . import models
from django.urls import reverse
from tweetapp.forms import AddTweetForm, AddTweetModelForm
def listtweet(request):
all_tweets = models.Tweet.objects.all()
tweet_dict = {"tweets":all_tweets}
return render(request,'tweetapp/listtweet.html',context=tweet_dict)
def addtweet(request):
if request.POST:
nickname = request.POST["nickname"]
message = request.POST["message"]
models.Tweet.objects.create(nickname=nickname, message=message)
return redirect(reverse('tweetapp:listtweet'))
else:
return render(request,'tweetapp/addtweet.html')
def addtweetbyform(request):
if request.method == "POST":
form = AddTweetForm(request.POST)
if form.is_valid():
nickname = form.cleaned_data["nickname_input"]
message = form.cleaned_data["message_input"]
models.Tweet.objects.create(nickname=nickname, message=message
)
return redirect(reverse('tweetapp:listtweet'))
else:
print("error in form")
return render(request,'tweetapp/addtweetbyform.html', context={"form":form})
else:
form = AddTweetForm()
return render(request,'tweetapp/addtweetbyform.html', context={"form":form})
def addtweetbymodelform(request):
if request.method == "POST":
form = AddTweetModelForm(request.POST)
if form.is_valid():
print(form.cleaned_data)
return redirect(reverse('tweetapp:listtweet'))
else:
print("error in form")
return render(request,'tweetapp/addtweetbymodelform.html', context={"form":form})
else:
form = AddTweetModelForm()
return render(request,'tweetapp/addtweetbymodelform.html', context={"form":form})
```
and i share my base.html
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'logout' %}?next=/">Logout</a>
</li>
在您的
urls.py
中,您有两条“”路线。那是 path("",include('tweetapp.urls'))
和 path("",include('django.contrib.auth.urls'))
您需要将帐户管理更改为类似这样的
urlpatterns = [
path("accounts/", include("django.contrib.auth.urls")),
]
您必须设置 root 才能注销。它对我有用。
为此,请将其添加到您的 urls.py 文件中:
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path("",include('tweetapp.urls')),
path("",include('django.contrib.auth.urls')),
path("logout/", auth_views.LogoutView.as_view(next_page=<redirect page>)),]