0

Views.py

from django.shortcuts import render
from django.template.context_processors import csrf
from  django.http import HttpResponseRedirect
from  django.shortcuts import render_to_response

from .models import studentDetails
from .forms import loginForm

# Create your views here.


def login(request):
    c = {}
    c.update(csrf(request))
    return render(request, "login.html", c)

def auth_view(request):
    username = request.POST.get("username", "")
    password = request.POST.get("password", "")

    q = studentDetails.objects.get(name=username)

    if q.password==password:
        return  HttpResponseRedirect("/student/accounts/loggedin")

    return  HttpResponseRedirect("/studemt/accounts/invalid")



def loggedin(request):
    username = request.GET.get("username")
    return  render(request, "loggedin.html", {"full_name": username})


def invalid(request):
    return  render(request, "invalid_login.html")


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

Urls.py

from django.conf.urls import url
from django.contrib import admin
from .views import (
    login,
    auth_view,
    loggedin,
    logout
    )


urlpatterns = [
    url(r"^accounts/login/$", login ,  name="login"),
    url(r"^accounts/auth/$", auth_view ,name="auth_view"),
    url(r"^accounts/loggedin/$", loggedin , name="loggedin"),
    url(r"^accounts/logout/$", logout, name="logout"),

]

i want to send username from auth_view to loggedin view but i don'y know how to do that. i have used username = request.GET.get("username") but it is not working. i want to show username in url also such that it looks like /student/username/ where username will change as different user login.

Rohit Chopra
  • 567
  • 1
  • 8
  • 24
  • **"where username will change as different user login"** I think, my answer will help you http://stackoverflow.com/questions/38781442/how-to-rewrite-base-url-in-django-to-add-logged-in-username-in-the-url-of-all-pa/38895767#38895767 – Ivan Semochkin Aug 21 '16 at 08:48
  • in my quesion auth_view is not rendering any page it is simply checking data and it want to send username from auth_view to loggedin view. – Rohit Chopra Aug 21 '16 at 08:53
  • You would usually store that in a [session](https://docs.djangoproject.com/en/1.10/topics/http/sessions/). Or use [djangos auth system](https://docs.djangoproject.com/en/1.10/topics/auth/default/) instead of rolling your own. – mata Aug 21 '16 at 09:00
  • can you tell me how to store session? – Rohit Chopra Aug 21 '16 at 09:02
  • @RohitChopra - you should read the [link] (https://docs.djangoproject.com/en/1.10/topics/http/sessions/) I've pointed to. [this](http://www.tutorialspoint.com/django/django_sessions.htm) also seems decent. – mata Aug 21 '16 at 09:14

1 Answers1

0

You should pass parameter in url first:

url(r'^student/(?P<username>\w+)/$', views.userpage, name='userpage)

But better use pk field or something with name + pk, as url parametr, because username can be duplicate.
Now you can pass this parameter in view and don't hardcore url, use reverse with url name instead.

def auth_view(request):
    username = request.POST.get("username", "")
    password = request.POST.get("password", "")

    q = studentDetails.objects.get(name=username)

    if q.password==password:
        return  HttpResponseRedirect(reverse('userpage', args=[q.username]))

    return  HttpResponseRedirect(reverse('invalid'))
Ivan Semochkin
  • 8,649
  • 3
  • 43
  • 75
  • i have change view and urls accordingly but it is not working **error:** `Reverse for 'loggedin' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] ` **change url :** `url(r'^(?P)/$', loggedin, name='loggedin'),` **change views.py** `def auth_view(request): username = request.POST.get("username", "") password = request.POST.get("password", "") q = studentDetails.objects.get(name=username) if q.password==password: return HttpResponseRedirect(reverse('loggedin'), args=[q.username]) return HttpResponseRedirect("/studemt/accounts/invalid")` – Rohit Chopra Aug 21 '16 at 09:10
  • ah yes, there was typo in `reverse` function, `args` must locate in `reverse` – Ivan Semochkin Aug 21 '16 at 09:13
  • what traceback sayed? – Ivan Semochkin Aug 21 '16 at 09:24
  • `NoReverseMatch at /student/accounts/auth/ Reverse for 'loggedin' with arguments '()' and keyword arguments '{'username': 'arun'}' not found. 0 pattern(s) tried: []` – Rohit Chopra Aug 21 '16 at 09:28
  • seems you use old url `/student/accounts/auth/ ` instead of `url(r'^(?P)/$` Also in previous traceback you forgot `\w+` regexp pointer. It should looks like this `url(r'^(?P\w+)/$` – Ivan Semochkin Aug 21 '16 at 09:31
  • urls.py `urlpatterns = [ url(r"^accounts/login/$", login , name="login"), url(r"^accounts/auth/$", auth_view ,name="auth_view"), url(r"^accounts/logout/$", logout, name="logout"), url(r'^(?P\w+)/$', loggedin, name='loggedin'), ]` **views.py** `def auth_view(request): username = request.POST.get("username", "") password = request.POST.get("password", "") q = studentDetails.objects.get(name=username) if q.password==password: return HttpResponseRedirect(reverse('loggedin', kwargs={"username": username})) ` – Rohit Chopra Aug 21 '16 at 09:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121452/discussion-between-baterson-and-rohit-chopra). – Ivan Semochkin Aug 21 '16 at 09:45