I am developing a tweet application and while I can log in during the authentication process, my logout function does not work. I leave my source codes to explanation. I'm waiting for your help
The error I get is as follows: (http://127.0.0.1:8000/logout/?next=/) and i get This site cannot be reached error! More precisely, I cannot access the page I want. When I click on logout, I have to log out of the account. I should also mention that I am using Python 3.12.0 and Django.
my urls.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')),]
my views.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>
THX GUYS I SOLVED PROBLEM WITH THIS CODES:
```
<form method="post" action="{% url 'logout' %}">
{% csrf_token %}
<button class="nav-link"
type="submit">Logout</button>
</form>
```
In your
urls.pyyou have two routes for "". That ispath("",include('tweetapp.urls'))andpath("",include('django.contrib.auth.urls'))You need to change the one for the account management to something like this
Refer to Django's documentation