Redirect to specifc details page not working

Hello,
I am trying to redirect the user to details page of an item only if he is logged in. I read a lot of topics, even chatGPT could helped me. I created even a custom LoginView but it still redirect the user not to the details page of the item, but to the home page. Here is some code:

This is the html for the item. At the end I have a button for going to details page.

{% if itemshop_list %}
        <h2 class="title">Items for sale</h2>
        <div class="custom-main">
            {% for item in itemshop_list %}
                <div class="cards">
                    <div class="card">
                        <div class="img">
                            <img src="{{ item.main_photo.url }}" alt="No Photo"/>
                        </div>
                        <div class="text">
                            <h2>
                                {{ item.title }}
                            </h2>
                            <ul>
                                <li>Price: {{ item.price }} BGN</li>
                            </ul>
                            <button><a href="{% url 'details_petshop' item.pk %}?next={{ request.path }}">Details</a></button>
                        </div>
                    </div>
                </div>
            {% endfor %}
        </div>
    {% else %}

The main settings.py. For urls I am have only one for using sign up format. But I a have custom log in template.

LOGIN_REDIRECT_URL = 'home'

AUTH_USER_MODEL = 'accounts.CustomUser'

For clarification - the user can go to a page while not authenticated.

If they click on a certain link on that page, you want them to be sent to the login page, and after they authenticate, you want them to be redirected to the page they were originally going to.

Is that correct?

If so, please show the view for the page that they are trying to get to.

Yes, that’s correct.

Here is the view for the page they are going to:

class PetShopDetails(LoginRequiredMixin, DetailView):
    model = ItemShop
    template_name = 'pet_shop_detail.html'

And this is the urls:

urlpatterns = [
    path('', PetShopView.as_view(), name='petshop'),
    path('create/', PetShopCreate.as_view(), name='create_petshop'),
    path('<int:pk>/', PetShopDetails.as_view(), name='details_petshop'),
    path('<int:pk>/edit/', PetShopEdit.as_view(), name='edit_petshop'),
    path('<int:pk>/delete/', PetShopDelete.as_view(), name='delete_petshop'),
]

What is your LOGIN_URL setting?

I suggest you remove the “next” parameter from your button link - Django is going to automatically add that when the mixin redirects your request to the login page.

1 Like

Ok, nice. Now when you mentioned LOGIN_URL, I dont have it explicit in settings.py but I found my error. In my custom template for login I had this:

{% block content %}
    <div class="login-box">
        <h2>Login Here</h2>
        <form method="post" action="{% url 'login' %}">
            {% csrf_token %}
            {{ form.as_p }}
            <button type="submit" class="btn-primary">Log In</button>
        </form>
    </div>
{% endblock %}

When I remove the url link from action, everythin works now :slight_smile: Thanks :slight_smile:

So it is like this right now

{% block content %}
    <div class="login-box">
        <h2>Login Here</h2>
        <form method="post" action="#">
            {% csrf_token %}
            {{ form.as_p }}
            <button type="submit" class="btn-primary">Log In</button>
        </form>
    </div>
{% endblock %}