Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6773973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:43:54+00:00 2026-05-26T15:43:54+00:00

Hi Stackoverflow people, I am trying to write my own contact form in Django,

  • 0

Hi Stackoverflow people,

I am trying to write my own contact form in Django, where users can write messages to me and the message will be emailed and saved in the DB for tracking.

But somehow, the model.save() won’t save anything. When I check the entries with Admin, the Contact table is empty. I also do not get any error messages.

The sending of the message hasn’t been fully implemented yet.

To test the code, I set up some status messages in the if/else branch, but I do not get any of the statement – so the code seems to be neglected. But I do not understand why? Any suggestions?

I am not sure if I hand over the request variable between the views.py and forms.py correctly. Could this be the issue?

Thank you for your suggestions.

models.py

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.conf import settings
from django.core.urlresolvers import reverse
from django.core.mail import send_mail

import datetime, random

class Contact(models.Model):
"""
Contact class is the model for user messages. 
Every message will be sent.
"""
CATEGORIES = (
        ('Suggestion', 'I have a suggestion'),
        ('Feedback', 'General feedback'),
        ('Complaint', 'You should improve ...'),
        # ('', ''),
        )

category = models.CharField(_('Message Category'),
                            max_length=10, 
                            choices=CATEGORIES)

subject = models.CharField(_('Message Subject'), 
                            max_length=255,)

sender = models.EmailField(_('Email Address'),)

message = models.TextField(_('Message Box'),)

# date and ansbwered are not visible to the user
timeOfMessage = models.DateTimeField(_('Time of sending'), blank=True, null=True)

answered = models.BooleanField(_('Answered?'),
                               default=False,)

def __unicode__(self):
    return '%s' % self.sender

def send_and_save(self):

    subject_new = ':'.join(self.category, self.subject)

    send_mail(subject_new,
              message,
              sender, 
              'info@future_domain_address.com')
    return True

forms.py

from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _
from contact.models import Contact
import datetime

class ContactForm(ModelForm):
    class Meta:
        model = Contact
        exclude = ('answered', 'timeOfMessage')

    def save_and_email(request):
       if request.method == 'POST':
           form = self(request.POST)
           if form.is_valid():
            # contact.cleaned_data()
              contact = form.save(commit=False)
              contact.timeOfMessage = datetime.now()
              contact.answered = False
              contact.save()
              print "was here"
              return True
           else: 
              print "Saving Failed"
       else:
           print "POST failed"
       return False

views.py

from django.views.generic.simple import direct_to_template
from django.shortcuts import redirect, get_object_or_404, render_to_response
from django.utils.translation import ugettext as _
from django.http import HttpResponseForbidden, Http404, HttpResponseRedirect, HttpResponse
from django.template import RequestContext
from django.core.mail import BadHeaderError
from contact.forms import ContactForm 

def contact(request):
   if request.method == 'POST':
       try:
           contactform = ContactForm()
           contactform.save_and_email
       except BadHeaderError:
           return HttpResponse(_('Invalid header found.'))
       return HttpResponseRedirect('/contact/thankyou/')

   return render_to_response('contact/contact.html', {'form': ContactForm()},
        RequestContext(request))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:43:54+00:00Added an answer on May 26, 2026 at 3:43 pm

    I see two things here. On the view the method is not called. The method needs the () to be called.

    Then the save_and_email method needs some corrections. First of all needs the self argument, or convert it to a .
    My suggestion is as follows.

    def save_and_email(self):
        if self.is_valid():
           contact = self.save(commit=False)
           contact.timeOfMessage = datetime.now()
           contact.answered = False
           contact.save()
           return True
        else: 
           return False
    

    And the view:

    def contact(request):
       if request.method == 'POST':
               contactform = ContactForm(request.POST)
               if contactform.save_and_email():
                  return HttpResponseRedirect('/contact/thankyou/')
    
       return render_to_response('contact/contact.html', {'form': ContactForm()},
            RequestContext(request))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello people of stackoverflow. I have a problem. I'm trying to set up my
I'm trying to write a tool that will take as input some C code
Hi Stackoverflow people, I am working on an organisation registration, in which orgs can
i'm trying to make my own markdown-able textarea like Stackoverflow has done. The goal
I'm trying to create a form that pops-up an error when people insert a
I'm trying to make my contact form submission process work equally well whether the
Great people of StackOverflow, save my ass please :) I've been trying to get
Hi Stackoverflow people, I am confused with m2m queries in Django. I have a
Hi Stackoverflow people, I would like to change the m2m widget in the admin
Hello again Stackoverflow people! Assume I have these words: smartphones, smartphone I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.