Home > Net >  Passing Id and Context in Django Redirect
Passing Id and Context in Django Redirect

Time:06-20

I want to pass a model to a html page as context when I login into an account. My Home page url comes with user id as a url parameter. But i cant pass any context in redirect

views.py

  from django.contrib.auth import authenticate,login,logout
from django.contrib import messages

from django.shortcuts import redirect, render
from django.http import HttpResponse
from .models import users
from home.models import Posts
 
def f1(request):
    Post = Posts.objects.all()
    context = {'Post':Post}
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        uz = authenticate(request,username=username, password=password)
        if uz is not None:
            login(request,uz)
            id = users.objects.filter(name=username).values('id')[0]['id']
            return redirect('home',id)  # This Works Fine
            return redirect('home',id,context) # This is not Working
        else:
            messages.error(request,"Wrong Credentials")
    return render(request,'login.html')

urls.py

 from django.contrib.auth import logout
from django.urls import path
from . import views

urlpatterns=[
    path('<str:users_id>/',views.func,name="home"),
]

How can I pass the context? If I can't tell me an alternative way to do it.

#Thanks

CodePudding user response:

You're redirecting to another view, so you're passing data through your url. You can't put your context in it, so you can directly render your template :

return render(request, "home.html", context)

If you really want to redirect to another url, as your context only contains all Post objects, you can build it in your home view. Then you have :

f1 :

return redirect('home',id)

func :

context = {'Post': Posts.objects.all()}
return render(request, "home.html", context)

CodePudding user response:

@Balizok is correct: you can't pass context via the URL, without urlencoding it, which would be a bad idea for large amounts of data. If you need the home page to show posts or not show posts depending on whether the user has just logged in, you could do it in a few ways:


Show posts only if the user is logged in:

def home(request, id):
    context = {}
    if request.user.is_authenticated:
        context["Post"] = Posts.objects.all()
    return render(request, "home.html", context)

Show posts only if the user has just been redirected from the login page via a query parameter:

from django.url import reverse

def f1(request):
    # ... log the user in etc.
    url = reverse("home", id)   "?show_posts=true"
    return redirect(url)
def func(request, id):
    context = {}
    if request.GET.get("show_posts") == "true":
        context["Post"] = Posts.objects.all()
    return render(request, "home.html", context)

Show posts only if the user has just been redirected from the login page via the session:

from django.url import reverse

def f1(request):
    # ... log the user in etc.
    request.session["show_posts"] = True
    return redirect("home", id)
def func(request, id):
    context = {}
    if request.session.get("show_posts"):
        context["Post"] = Posts.objects.all()
    return render(request, "home.html", context)

CodePudding user response:

Add the id to your context dictionary and pass to redirect. Something like

context = {'Post':Post, 'Id':id}

return redirect('home', context)
  • Related