user11848547
user11848547

Reputation:

Django urls giving me different direction

I am working on Django project where I am implementing the in-build authentication framework. I created urls directly to in-build views.

I am supposed to visit the following url http://127.0.0.1:8000/account/login/ which redirects me to the following urls and supposed to choose 'LoginView' and it's corresponding template

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('', views.dashboard, name='dashboard'),
]

Here is the template code

{% block content %}
    <h1>Log-in</h1>
    {% if form.errors %}
        <p>
            Your username and password didn't match. please try again.
        </p>
    {% else %}
        <p>Please, use the following form to log-in: </p>
    {% endif %}
    <div class="login-form">
        <form action="{% url 'login' %}" method="post">
            {{form.as_p}}
            {% csrf_token %}
            <input type="hidden" name="next" value="{{next}}">
            <p><input type="submit" value="Log-in"></p>
        </form>
    </div>
{% endblock %}

when I submitted the credentials I am getting the following error

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/accounts/profile/
Using the URLconf defined in xxxxxxxxxx.urls, Django tried these URL patterns, in this order:

admin/
account/
The current path, accounts/profile/, didn't match any of these.

Please help me with this.

Upvotes: 2

Views: 219

Answers (2)

SwapnilBhate
SwapnilBhate

Reputation: 274

Upon successful login, Django has to redirect user to certain page. This setting is accessible in your project's settings.py.

As per django documentation, default redirect URL is set as /accounts/profile/. Hence you must be receiving the error as application is following the default value and trying to redirect the user to default URL upon successful login.

If not aleady, try adding below line to your settings.py.

LOGIN_REDIRECT_URL = '/'

Considering you would like to redirect user to your dashboard view upon successful login. If not, change the URL with the desired value and it should work.

Upvotes: 2

michjnich
michjnich

Reputation: 3395

I suspect you have your login.html and logout.html templates in the wrong folder. Django requires them to be in a directory named registration by default.

Just to clarify. In my user_profile app, I have

templates
|-- registration
  |-- login.html
  |-- logout.html
|-- user_profile
  |-- signup.html
  |-- userprofile_form.html

Upvotes: -1

Related Questions