Home > Blockchain >  Django routing/pathing to multiply apps
Django routing/pathing to multiply apps

Time:04-22

Hi i know there is many of these asking about routing from a veiw to a new app in django. I have looked at a lot of them. And figured out to use app_name = 'name' and to use named routes for my html templates makes everything easier for sure. When i try to use a app_name:named-route i get this :

ValueError at /recipedashboard The view recipeApp.views.dashboard didn't return an HttpResponse object. It returned None instead.

I have gotten the name_app:named-route to send me to the second app, but how do i pass the session to the new app?

userApp veiws:

from django.shortcuts import render, redirect
from django.contrib import messages
from .models import User as LoggedUser

# Create your views here.

def index(request):
    return render(request, 'index.html')

def register(request):
    if request.method == "GET":
        return redirect('/')
    errors = LoggedUser.objects.validate(request.POST)
    if len(errors) > 0:
        for er in errors.values():
            messages.error(request, er)
        return redirect('/')
    else:
        new_user = LoggedUser.objects.register(request.POST)
        request.session['user_id'] = new_user.id
        messages.success(request, "You have successfully registered")
        return redirect('/success')

def login(request):
    if request.method == "GET":
        return redirect('/')
    if not LoggedUser.objects.authenticate(request.POST['email'],
        request.POST['password']):
        messages.error(request, "Invalid Email/Password")
        return redirect('/')
    user = LoggedUser.objects.get(email=request.POST['email'])
    request.session['user_id'] = user.id
    messages.success(request, "You have successfully logged in!")
    return redirect('/success')

def logout(request):
    request.session.clear()
    return redirect('/')

def success(request):
    if 'user_id' not in request.session:
        return redirect('/')
    context = {
        'user' : LoggedUser.objects.get(id=request.session['user_id'])
    }
    return redirect("recipe:recipe-dashboard")

recipeApp urls.py:

from django.urls import path
from . import views as recipeViews
app_name ='recipe'


urlpatterns = [
    path('dashboard', recipeViews.dashboard, name= "recipe-dashboard" )
] 

recipeApp.Views :

from django.shortcuts import render, redirect
from userApp.models import User as LoggedUser

# Create your views here.
def dashboard(request):
    if 'user_id' not in request.session:
        return redirect('/')
    user = LoggedUser.objects.get(id=request.session['user_id'])
    context = {
        'user' : user
    }
    render (request, 'dashboard.html', context)

CodePudding user response:

Your dashboard view is not returning anything.

You need to return the results of render. Like

from django.shortcuts import render, redirect
from userApp.models import User as LoggedUser

# Create your views here.
def dashboard(request):
    if 'user_id' not in request.session:
        return redirect('/')
    user = LoggedUser.objects.get(id=request.session['user_id'])
    context = {
        'user' : user
    }
    return render (request, 'dashboard.html', context)

There is no need to deal with passing a session. The framework will handle that for you if the apps are all under the same project.

  • Related