Contact fields were not appearing in my website


#1

contact page were showing only “Contact label” and “Submit” button, but all the fields between ( name, email, and content ) were hidden.

contact.html

{% extends 'layouts/base.html' %}

{% block title %}Contact - {{ block.super }}{% endblock %}
{% block content %}
    <h1>Contact</h1>
    <form role="form" action="" method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
{% endblock %}

#2

What does your forms.py and views.py look like?


#3

Thanks for the fast reply …

forms.py

from django import forms

class ContactForm(forms.Form):
    contact_name = forms.CharField()
    contact_email = forms.EmailField()
    content = forms.CharField(widget=forms.Textarea)

    # adding some better label names 

    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        self.fields['contact_name'].label = "Your name:"
        self.fields['contact_email'].label = "Your email:"
        self.fields['content'].label = "What do you want to say?"

views.py

from django.template.loader import get_template
from django.core.mail import EmailMessage
from django.template import Context
from the_start.forms import ContactForm
from django.shortcuts import render, redirect
from the_start.models import Thing

# Create your views here.
def index(request):
    things = Thing.objects.all()
    # this is your new view
    return render(request, 'index.html', {'things': things,})

def thing_detail(request, slug):
    # grab the object
    thing = Thing.objects.get(slug=slug)
    # and pass to the template
    return render(request, 'things/thing_detail.html', { 'thing': thing, })

def browse_by_name(request, initial=None):
    if initial:
        things = Thing.objects.filter(name__istartswith=initial).order_by('name')
    else:
        things = Thing.objects.all().order_by('name')
    return render(request, 'search/search.html', { 'things': things, 'initial': initial, })

def contact(request):
    form_class = ContactForm
    
    # new logic!
    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = form.cleaned_data['contact_name']
            contact_email = form.cleaned_data['contact_email']
            form_content = form.cleaned_data['content']

            # email the profile with the contact info
            template = get_template('contact_template.txt')


            context = Context({ 'contact_name' : contact_name, 'contact_email' : contact_email, 'form_content' : form_content, })

            content = template.render(context)

            email = EmailMessage('New Contact form submission', content, 'Your website <[email protected]>', ['[email protected]'], headers = {'Reply-To': contact_email })
            
            email.send()
            return redirect('contact')

    return render(request, 'contact.html', {'form': form_class, })

#4

Huh. Did it show before you added in the logic that deals with POST submissions (the # new logic! part?)


#5

Sorry i thought i mentioned it before in my first post… it worked fine at first, but in the middle of my project i wanted to know if i commented some code (sections on my website) without delete it in order to see what happens in my website.
The basic idea i want to see my website showing only home page and about page, so i commented all the other parts and it worked just fine, then i return everything back to normal and uncommented everthing back. when i checked the contact page i found it like this.


#6

Hm. Are you tracking changes with git? Could you look at your git history and see if something got changed accidentally?


#7

I’m having the same issue. No errors just not showing up. Ele were you able to figure it out?


#8

After hours of googling and reworking the same code over and over I realized that the url for the contact needs to be at the top of the list of urls. Once I moved it the fields showed up.


#9

Can you expand on what you mean on this, so I can make sure I don’t have a bug?


#10

I had the same error! And your solution worked! I moved the URL to the top of the list and now my form fields are showing up. Before I also only had the header and the submit button showing up.


#11

Yay! Glad to hear it.