register redirect home python django

Viewed 23

Can anyone explain why my code does not redirect me to home.html after successful registration? I'm creating custom login and registration forms. Let me know if there is more code you need to see.

from django.shortcuts import render, redirect 
from django.views.generic import TemplateView, CreateView
from .forms import JoinForm



class HomeView(TemplateView):
    template_name = "home.html"
    
    
class JoinView(CreateView):
    form = JoinForm
    success_url = "home"
    template_name = "registration/join.html"
    
    
def join(request):
    if request.method == "POST":
        form = JoinForm(request.POST or None)
        if form.is_vaild():
            form.save()
        return redirect('home')
    else:
        return redirect('join')  
    
    
    
class LoginView(TemplateView):
    template_name = "login.html"
    
    

My URLs

from django.urls import path 
from .views import HomeView, LoginView, JoinView

urlpatterns = [
    path('', HomeView.as_view(), name="home"),
    path('join/', JoinView.as_view(), name="join"),
    path('login', LoginView.as_view(), name="login"),
    
]

Models.py

from django.db import models



class Join(models.Model):
    fname = models.CharField(max_length=100, blank=True)
    lname = models.CharField(max_length=100, blank=True)
    email = models.EmailField(max_length=100, blank=True)
    age = models.IntegerField()
    password1 = models.CharField(max_length=50, blank=True)
    
    
    
    def __str__(self):
        return (self.fname + "  " + self.lname)

form.py

from django import forms
from .models import Join



class JoinForm(forms.ModelForm):
    class Meta:
        model = Join
        fields = ['fname', 'lname', 'email', 'password1', 'age']

join.html

{% extends 'base.html' %}


{% block content %}

<h2>Become a Member</h2>
<form method="POST" action="{% url 'join' %}" \>
    {%csrf_token%}
    <div class="form-group">
      <input type="email" class="form-control" placeholder="Email Address" name="email">
    </div>
    <div class="form-group">
      <input type="text" class="form-control" id="exampleInputPassword1" placeholder="First name:" name="fname">
    </div>
    <div class="form-group">
        <input type="text" class="form-control" id="exampleInputPassword1" placeholder="Last name:" name="lname">
      </div>
      <div class="form-group">
        <input type="text" class="form-control" id="exampleInputPassword1" placeholder="Age:" name="age">
      </div>
      <div class="form-group">
        <input type="Password" class="form-control" id="exampleInputPassword1" placeholder="Password:" name="Password1">
      </div>
    
    <button type="submit" class="btn btn-primary">Join!</button>
  </form>
{% endblock content %}

Here is my base.html


<!doctype html>
{% load static %}
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">

    <title>Hello, world!</title>
  </head>
  <body>
    <header>
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <a class="navbar-brand" href="{% url "home" %}">Nails</a>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
              <ul class="navbar-nav">
                <li class="nav-item active">
                  <a class="nav-link" href="{% url 'home' %}">Home</a>
                </li>
                <li class="nav-item active">
                    <a class="nav-link" href="{% url 'login' %}">Log In</a>
                  </li>
                  <li class="nav-item active">
                    <a class="nav-link" href="{% url 'join' %}">Join</a>
                  </li>
               
              </ul>
            </div>
          </nav>
    </header>
    {% if user.is_authenticated %}
    <p>Hi {{ user.username }}!</p>
    <p><a href="{% url 'logout' %}">Log Out</a></p>
    {% else %}
    <p>You are not logged in</p>
    {% endif %}

{% block content %}
{% endblock content %}

    
  </body>
</html>

Please let me know If there is anything more I can do to help.

1 Answers

You join view is not good. render function take a request, a template and a context as paramters for making the rendering. You try to pass the url name. use redirect instead.

from django.shortcuts import redirect

def join(request):
    if request.method == "POST":
        form = JoinForm(request.POST or None)
        if form.is_vaild():
            form.save()
        return redirect('home')
    else:
        return render(request, 'registration/join.html')  

and in your urls:

from .views import HomeView, LoginView, join

urlpatterns = [
    path('', HomeView.as_view(), name="home"),
    path('join/', join, name="join"),
    path('signin', LoginView.as_view(), name="signing"),
    
]

Not good practice to not process the post request in the same function view you used for GET rendering. In the case your form is not good, with this pattern, you can not pass easely form errors to template.

An idea of view you can do it with django generic view is:

class JoinView(CreateView):
    form = JoinView
    success_url = "home"
    template_name = "registration/join.html"

# And in your urls
from .views import HomeView, LoginView, JoinView

urlpatterns = [
    path('', HomeView.as_view(), name="home"),
    path('join/', JoinView.as_view(), name="join"),
    path('signin', LoginView.as_view(), name="signing"),
    
]
Related