Search code examples
djangodjango-modelsdjango-formsdjango-viewsdjango-users

How to fix automatic login in Django?


How to fix automatic login in Django?

When Someone goes to someone profile by typing URL like http://127.0.0.1:8000/profile/1 so when the page loaded it will automatic login to person profile, for example, the admin had 1 profile with pk=1 so if some type URL like this http://127.0.0.1:8000/profile/1 he will log in to admin without password username

I Want Like This No Body Can log in To Profile with a link and He Can Only View Someone Profile Not Login To Someone Profile

Here is my Views.py

def profile_detail(request,pk):
    user = get_object_or_404(User, pk=pk)
    model = user_register_model()
    return render(request,'profile_detail_view.html',{'user':user,'model':model,})

#register  view


def user_reg(request):
    register = False
    if request.method == 'POST':
        form = user_register_form(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.set_password(user.password)
            user.save()

            register = True

            if register == True:
                return HttpResponseRedirect("/accounts/login")

        else:
            print(form.errors)
    else:
        form = user_register_form()

    context = {'reg':form,}
    return render(request,'signup.html',context)

Here is my Base.html

  {% if user.is_authenticated %}
  <li class="nav-item">

    <a class="nav-link navaour" href="{% url 'profile' pk=user.pk %}"><i class="fa fa-user"></i>&nbsp; Profile</a>
  </li>
  <li class="nav-item">

    <a class="nav-link navaour" href="{% url 'logout' %}"><i class="fa fa-power-off"></i>&nbsp; Logout</a>
  </li>

  {% else %}
  <li class="nav-item">

    <a class="nav-link navaour" href="{% url 'register' %}"><i class="fa fa-check-square-o"></i>&nbsp; Sign up Free</a>
  </li>    
  <li class="nav-item">

    <a class="nav-link navaour" href="{% url 'login' %}"><i class="fa fa-user"></i>&nbsp; Login</a>
  </li>
  {% endif %}    

Here is my Profile_Detail_View.html

<div class="row">
    <div class="col-sm-3 col-md-2 col-5">
        <label style="font-weight:bold;">Full Name</label>
    </div>
    <div class="col-md-8 col-6">
        {{user.username}}
    </div>
</div>
<hr />

<div class="row">
    <div class="col-sm-3 col-md-2 col-5">
        <label style="font-weight:bold;">Join Date</label>
    </div>
    <div class="col-md-8 col-6">
        {{ model.join_date|date:"M d,Y" }}
    </div>
</div>
<hr />


<div class="row">
    <div class="col-sm-3 col-md-2 col-5">
        <label style="font-weight:bold;">Email</label>
    </div>
    <div class="col-md-8 col-6">
        {{ user.email }}
    </div>
</div>

Here is my Models.py

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils import timezone

# Create your models here.
class user_register_model(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    join_date = models.DateTimeField(default=timezone.now)

Here is my Urls.py

from . import views
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', views.index,name='index'),
    path('accounts/signup/', views.user_reg,name='register'),
    path('profile/<int:pk>',views.profile_detail,name='profile')

]

Here is The Gif Video:

enter image description here


Solution

  • i missed a request in base.html

    i need to replace this line:

    {% if not user.is_authenticated %}
    

    To This:

    {% if not request.user.is_authenticated %}