Search code examples
pythondjangosqlitemobile-website

Can't get 'Form' working in Django


I am creating a site using Django/Python. I have only just begun the registration page where a user will input their info and the data should flow into the sqlite3 DB which comes with Django. I have since yesterday evening (been just under 24 hours now) trying to figure out why my forms aren't showing up. I tried attaching what I'm getting vs what I'm aiming for but don't have enough reputation points so please visit: https://www.youtube.com/watch?v=yfjhLKL-_5Q 9.09mins for what I'm aiming for. Please note: The word 'welcome' on the youtube vid has been replaced with 'Thank you' by me. This code is in my views.py.

The issue is that the small boxes which a user would type in their fullname and email in the video (which I have replaced with Profession, first_name, last_name etc) are not appearing at all... instead I am getting just 'Thank you' appearing along with my 'username' (jmitchel3 equivalent) and registration button but no request for text boxes. My code is below.

I initially thought that my forms were setup incorrectly so I compared my code to the page that's successful and no luck. I did the same for models but couldn't see anything there either ...

models.py:

from django.db import models

# Create your models here.
class Register(models.Model):

    Profession = models.CharField(max_length=120)
    first_name = models.CharField(max_length=120)
    last_name = models.CharField(max_length=120)
    email = models.EmailField(max_length = 120, unique = True)
    phone_number = models.CharField(max_length=120)
    instgram_ID = models.CharField(max_length=120, blank=True, null=True)
    facebook_ID = models.CharField(max_length=120, blank=True, null=True)
    twitter_ID = models.CharField(max_length=120, blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return self.email

    forms.py

    from django import forms

from .models import Register

class RegisterForm(forms.ModelForm):
    class Meta: 
        model = Register
        fields = ['first_name', 'last_name', 'email', 'phone_number', 'instgram_ID', 'facebook_ID', 'twitter_ID']

    def clean_email(self):
        email = self.cleaned_data.get('email')
        email_base, provider = email.split("@")
        domain, extension = provider.split('.')
        #if not domain = 'USC':
        #   raise forms.ValidationError("Please make sure you use your USC email.")
        #if not extension == "edu":
        #   raise forms.ValidationError("Please use a valid .EDU email address")

            #return email

        def clean_full_name(self):
            full_name = self.cleaned_data.get('full_name')
            #write validation code.
            return full_name

        admin.py

        from django.contrib import admin

    # Register your models here.

    from .forms import RegisterForm
    from .models import Register

    class RegisterAdmin(admin.ModelAdmin):
        list_display = ["__unicode__", "timestamp", "updated"]
        form = RegisterForm
        #class Meta:
        #   model = Register
    )

    admin.site.register(Register, RegisterAdmin

views.py

from django.shortcuts import render

from .forms import RegisterForm
# Create your views here.
def home(request):
    title = 'Welcome'
    form = RegisterForm(request.POST or None)
    context = {
    "title": title, 
    "form": form 
    }

    if form.is_valid():
        instance = form.save(commit=False)
        if not instance.full_name:
            instance.full_name = "Justin"
        instance.save

    context = {
        "title": "Thank you"
        }

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

home.html:

<h1>{{title}}</h1>

{{ user }}
{{ request.user }}

<form method ='POST' action=''>{% csrf_token %}

{{ form }}

<input type='submit' value = 'Register'>

<!-- <input> type='text'> -->
<!-- <input> -->

    </form>

It's been a while since I have been on here so if I need to add anything else, let me know..


Solution

  • You have an indentation error. The part where context is replaced iwth just {"title": "Thank you"} should be indented within the is_valid() block. Where it is now, it is replacing the earlier definition of context, which has the result that you're not sending the form to the template.