0

I read this. Django: Redirect to previous page after login But in my case it doesn't work. Also in my settings.py, I have

LOGIN_REDIRECT_URL = '/home' #it means the home view

I have done this in my base.html

<li><a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">Patient's login</a></li>

and also have added this to my login.html

<input type="hidden" name="next" value="{{ next }}" />

But still it doesn't redirect to the page the user was after login. Also when the user is logged in, how can I show at the top right corner on the nav bar something like "Welcome #"

# being whatever the username is.

And when he's logged out, it doesn't show such thing. And I'm using an CustomUser(AbstractUser) in models.py

How can i do this?

login.html

<form action="{% url 'login' %}" method="post" enctype="multipart/form-data">
            {% csrf_token %}
                <h1>Login Form</h1>

                <div class="imgcontainer">
                    <img src="{% static 'student/patient.jpg' %}" alt="Avatar" class="avatar">

                </div>

                <div class="username">
                    {{ form.username.label_tag }}
                    {{ form.username }}                 
                </div>

                <div class="password">
                    {{ form.password.label_tag }}
                    {{ form.password }}
                </div>

                <div class="submitb">
                    <input type="submit" value="Log In" name="mybtn">

                </div>

                <div class="resetb">
                    <input type="submit" value="Reset">
                    <a href="#forgotpwd">Forgot password?</a>
                </div>

                <input type="hidden" name="next" value="{{ next }}" />
            </form>
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
Shefali
  • 251
  • 6
  • 22
  • 1
    Please show your login view and template. Your link `{% url django.contrib.auth.views.login %}` is extremely out of date. It should be something like `{% url 'login' %}`, depending on your urls. – Alasdair Sep 21 '17 at 14:32
  • its the same thing...still doesn't achieve what i wanted to. – Shefali Sep 21 '17 at 14:34
  • You haven't shown your login view and template as I asked. – Alasdair Sep 21 '17 at 14:35
  • i'm using LoginForm. there's no login view. i'm adding the template. – Shefali Sep 21 '17 at 14:36
  • What do you mean "there is no login view"? There must be a login view, even if you are just using `LoginView` from Django. – Alasdair Sep 21 '17 at 14:41
  • i don't think that's even required. in project/urls.py we can directly import LoginForm from student.forms. why is it required? – Shefali Sep 21 '17 at 14:42
  • It's not working because you don't have a view. Change your `action` url. In the view get the link where it should redirects to by `request.POST['next']` and `render()` that page. – MD. Khairul Basar Sep 21 '17 at 14:44
  • I can't help any further, because you have not shown the view that is creating or handling the login form. Note that if you use the built in [`LoginView`](https://docs.djangoproject.com/en/1.11/topics/auth/default/#django.contrib.auth.views.LoginView), it will handle the redirect correctly. – Alasdair Sep 21 '17 at 14:46
  • By the way, are you using `TemplateView` ? Without a `view` how do you render the template ? :/ – MD. Khairul Basar Sep 21 '17 at 14:46
  • just read the first answer at the link i mentioned in my question. – Shefali Sep 21 '17 at 14:47
  • its an inbuilt functionality in django when we use its authentication system. – Shefali Sep 21 '17 at 14:48
  • Not sure about django but I know in Flask there's a `{{ current_user.is_authenticated }}` that you can check against. – Wayne Werner Sep 21 '17 at 17:52

0 Answers0