skip to Main Content

I am working on a Python Django project. I’m currently working on a way for myself to log in and create a product card for this project store. The create button should only appear when I am logged in. It’s just not appearing. I go to Inspect the web page and I don’t even see the code for my button. I have no idea what is happening and some help would be awesome!

This is the block of code that should be producing said button. If the user is authenticated, it’ll create this create button under each one of my categories. I originally had the for statement over the if statement and I switched it to see if that would change anything and nothing happened.

HTML

        {% if user.is_authenticated %}
            {% for category in categories %}
                <p>User is authenticated: {{ user.username }}</p> <!--LINE FOR DEBUGGING-->
                <p>Category: {{ category.name }}</p> <!--LINE FOR DEBUGGING-->
                <a class="create_button" href="{% url 'create_product' category.id %}">Create {{ category.name }}</a>
            {% endfor %}
        {% endif %}

One of the first things I did I was add some debugging lines to see if it actually understood I was logged in.

HTML

<div>
            {% if user.is_authenticated %}
                <p>User is authenticated: {{ user.username }}</p> <!-- LINE FOR DEBUGGING -->
            {% else %}
                <p>User is not authenticated</p> <!-- LINE FOR DEBUGGING -->
            {% endif %}
        </div>

That seemed to work perfectly fine. When I log in, I see a line of text that says I’m authenticated. I made sure to check my Urls.py as well.

path('create_product/<int:category_id>/', views.create_product, name='create_product'),
It certainly seems pretty alright to me, though I could be wrong. Lastly this is my create_product function. When I have finished creating said product it should return you to the home page.

PYTHON

def create_product(request, category_id):
    category = get_object_or_404(Category, pk=category_id)
    if request.method == "POST":
        form = ProductForm(request.POST, request.FILES)
        if form.is_valid():
            product = form.save(commit=False)
            product.category = category
            product.save()
            return redirect('home')
    else:
        form = ProductForm()
    return render(request, 'app/create_product.html', {'form': form, 'category': category})

I even have a css class for the button and set the z-index and color of it, so I know it should be appearing, but it simply isn’t. I even tried removing the z-index at some point and nothing changed.

CSS

.create_button {
    z-index: 1;
    color: blue;
}

I’m so confused. And to top it off if I type http://localhost:8000/create_product/1/ or http://localhost:8000/create_product/ to see if I can just directly go and create a product, it says the page ain’t even found. That it looked through my urls and the current path create_product/ didn’t match any of my urls. Maybe I’m overthinking this, idk. If I am a gentle slap on the face would be awesome. Thank you!

2

Answers


  1. It look like you didn’t provide the categories parameter in the template. When the templates are rendered, no buttons are added, If categories is None.

    Login or Signup to reply.
  2. I think the problem is you’re using forms but didn’t pass the form tags in your html. Or maybe it’s outside a loop so the form isn’t being processed. Don’t bite me if you did. I can only speculate from the snippet of codes you gave. Otherwise, code looks fine to me. ALso, problem could be with your imports…Also, do you have more than one apps installed sharing the same url paths? Problem could be with namespacing too. Django could be looking in the wrong app.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search