0

I wrote a small application for a social media site and I have a problem. I'm using {% if user.is_authenticated %} after I log in I'm getting the options in the navbar which should not be displayed for an authenticated user. The page keeps bouncing between two pages. I have made a video of the problem. Please have a look at it. Video.

Files tree image: File tree

base.html

<!DOCTYPE html>
{% load static %}
<html > 
    <head>
        <title>Colab</title>
        <link rel="stylesheet" type="text/css" href="{% static 'reg_sign_in_out/style.css' %}">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    </head>
    <body>
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <a class="navbar-brand" href="#">Colab</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
              <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
              <div class="navbar-nav">
                
                {% if user.is_authenticated %}
                <a class="nav-item nav-link" href="{% url 'posts:home' %}">Home</a>
                <a class="nav-item nav-link" href="{% url 'logout' %}">Logout</a>                

                {%else%}

                <a class="nav-item nav-link active" href="{% url 'index' %}">Home <span class="sr-only">(current)</span></a>
                <a class="nav-item nav-link" href="{% url 'reg_sign_in_out:user_login' %}">Login</a>
                <a class="nav-item nav-link" href="{% url 'reg_sign_in_out:registration' %}">Register</a>

                {% endif %}

                <!-- <a class="nav-item nav-link disabled" href="#">Disabled</a> -->
              </div>
            </div>
          </nav>
          <div class="container">
          {% block body_block %}
          
          {% endblock body_block %}
        </div>
    </body>
</html>

posts/urls.py

app_name = "posts"

urlpatterns = [
    path('home/',views.home,name='home'),

]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

reg_sign_in_out/urls.py

app_name = "reg_sign_in_out"

urlpatterns = [
    path('user_login/',views.user_login,name='user_login'),
    path('registration/',views.registration,name="registration"),

]

project_colab/urls.py (main)

urlpatterns = [
    path('',views.index,name="index"),
    path('',include('reg_sign_in_out.urls'),name="reg_sign_in_out"),
    path('',include('posts.urls'),name="posts"),
    path('admin/', admin.site.urls),
    path('logout/',views.user_logout,name='logout'),
    path('special/',views.special,name='special'),
]

posts/views.py

def home(request):

    post_creation = forms.PostsForm()

    if request.method == "POST":
        # name = request.POST.get('name')
        # time = timezone.now()
        # post_text = request.POST.get('post_text')

        # temp = Posts(name=name,time=time,post_text=post_text)
        # temp.save()
        post_creation = forms.PostsForm(request.POST,request.FILES)

        if post_creation.is_valid():          
            post_creation.save()
    
    postx = Posts.objects.all()

    return render(request,"posts/index.html",context={"post_info":postx,
                            "user":post_creation})

reg_sign_in_out/views.py

def index(request):
    return render(request,"index.html")

@login_required
def special(request):
    return HttpResponse("In!")

@login_required
def user_logout(request):
    logout(request)
    return HttpResponseRedirect(reverse('index'))  

@csrf_protect
def registration(request):

    registered = False
 
    if request.method == "POST":
        form = forms.UserForm(request.POST)
        profileform = forms.RegistrationForm(request.POST,request.FILES)

        if form.is_valid() and profileform.is_valid():
            
            user = form.save()
            print(user)
            user.set_password(user.password)
            user.save()

            profile = profileform.save(commit=False)
            profile.user = user
            
            profile.save()

            registered = True

        else:

            print(form.errors,profileform.errors)
            
            return render(request,"reg_sign_in_out/registration.html",{"tried":"True",
                                                    "registered":registered,
                                                   "profile_form":profileform,
                                                   "user_form":form,
                                                   })
            

    else:
        user = forms.UserForm()
        profileform = forms.RegistrationForm()

    return render(request,"reg_sign_in_out/registration.html",{"registered":registered,
                                                   "profile_form":profileform,
                                                   "user_form":user,
                                                   })


@csrf_protect
def user_login(request):

    if request.method == "POST":
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(username=username,password=password)
        if user:
            if user.is_active:
                login(request,user)
                return HttpResponseRedirect(reverse('index'))

        else:

            return render(request,"reg_sign_in_out/login.html",{'tried':'True'})

    else:
        return render(request,"reg_sign_in_out/login.html")

1 Answers1

0

I think the error in your file project_colab/urls.py (main) because you execute the function at the main of the project so, it affects whole the project so, I think you have to remove the function from urls.py in the main project and instead, try to put as following:
path('',include("index.urls")),
and in the posts/urls.py file you can put the function you has created and the path will be:
path('',views.index,name="index"),

Abdelhamed Abdin
  • 556
  • 1
  • 6
  • 19