0

I'm quite new for the web development using Python. I am using the following example (given in some of the questions in this website) python and HTML files in order to create a django form with Python 3.5.2:

urls.py

from django.conf.urls import patterns, url
import views

urlpatterns = patterns(
'',
url(r'^email/$',
    views.email,
    name='email'
    ),
url(r'^thanks/$',
    views.thanks,
    name='thanks'
    ),)

forms.py

from django import forms

class ContactForm(forms.Form):

    from_email = forms.EmailField(required=True)
    subject = forms.CharField(required=True)
    message = forms.CharField(widget=forms.Textarea)

views.py

from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from yourapp.forms import ContactForm


def email(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            try:
                send_mail(subject, message, from_email, ['[email protected]'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('thanks')
    return render(request, "yourapp/email.html", {'form': form})


def thanks(request):
    return HttpResponse('Thank you for your message.')

email.html

<form method="post">
    {% csrf_token %}
    {{ form }}
    <div class="form-actions">
      <button type="submit">Send</button>
    </div>
</form>

However, everytime I run the html file I can't get the form on the page. Instead I get the following screenshot:

Screenshot

What can be the problem? Thanks for the help!

3
  • 1
    This is not a question about forms, but a basic misunderstanding of how to use Django. You should go and do the introductory Django tutorial. Commented Nov 27, 2016 at 12:16
  • I second this. The Django tutorial is very good and will give you a much better idea of how Django works. Commented Nov 27, 2016 at 12:28
  • I was rushing for a project so I should've missed something.. I'll go through the tutorials again. Thanks for the help! Commented Nov 27, 2016 at 12:31

2 Answers 2

2

You can't run the HTML file on its own. You need to run the Django development server using this command:

python manage.py runserver

You should then be able to see the form at http://localhost:8000/email/

Sign up to request clarification or add additional context in comments.

Comments

0

The very first Django tutorial mentions that to test your application you need to start the development server through the following command:

$ python manage.py runserver

Check this for more details The development server

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.