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

  • Home
  • SEARCH
  • 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 363893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:23:10+00:00 2026-05-12T13:23:10+00:00

I am a primarily a PHP guy. How would I handle forms in django?

  • 0

I am a primarily a PHP guy. How would I handle forms in django? I managed to create a form, model and a view. Now I want to save my data into the database.

#forms.py
from django import forms
import datetime

class CommentForm(forms.Form):
    name = forms.CharField(initial='Your name')
    comment = forms.CharField(initial='You comment')
    commend_date = forms.DateField(initial=datetime.date.today);
    posted_by = forms.CharField(max_length=200)

And my view is like this:

#views.py
from django.shortcuts import render_to_response
from django.http import Http404
from django.template import Template, Context
from mysite.blog.models import Blog         #load a model
from mysite.blog.forms import CommentForm   #load a form


def index(request):
    try:
        blog_posts = Blog.objects.all().order_by('-post_date')[:10]

        if request.method == 'POST':
            form = CommentForm(request.POST)
        else:
            form = CommentForm()

    except Blog.DoesNotExist:
        raise Http404

    return render_to_response('blog/posts.html', locals())

Then my template (blog/posts.html) is like this:

{% extends "base.html" %}
{% block title %}Dummy Django Site{% endblock %}
{% block heading %}A sample header{% endblock %}
{% block content %}

    {% if blog_posts %}
        {% for post in blog_posts %}
        <b>{{post.title}}</b><br/>
        <b>{{post.post_date}}</b><br/>
        <b>{{post.posted_by}}</b><br/>
        {{post.content}}
        {% endfor %}
    {% endif %}

    <h2>A Sample Form</h2>
    <form action="" method="POST">
        {% for field in form %}
            <div class="fieldWrapper">
                {{ field.errors }}
                {{ field.label_tag }}: {{ field }}
            </div>
        {% endfor %}
        <p><input type="submit" value="Send message" /></p>
    </form>

{% endblock %}

{% block footer %}
<hr/>
Great footer!
{% endblock %}

In PHP, I would put in something in my controller like:

//from <form action="http://mysite.com/saveComment" method="POST" >
function saveComment() {
    $data['name'] = $_POST['name'];
    $data['comment'] = $_POST['comment'];
    $model->save($name);
}

How would I do that in Django? What where would I put the code?

Any reply would be greatly appreciated.

  • 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-12T13:23:10+00:00Added an answer on May 12, 2026 at 1:23 pm

    If you really need a lot of custom stuff, check out ModelForms, but Generic Views are sufficient for an awful lot of use cases.

    P.S. If you’re really doing Comment-related stuff, check out the provided Comments app in django.contrib.comments.

    UPDATE: Instead of defining your views and forms as you have above, use a ModelForm instead. This will allow to use code like the following to do the save:

    # Save a new Comment object from the form's data.
    >>> new_comment = my_comment_form.save()
    

    The change to your forms.py would change the definition of CommentForm to something like:

    from django.forms import ModelForm
    
    class CommentForm(ModelForm):
        class Meta:
            model = Comment  # Assumes you've imported your model somewhere
    

    UPDATE 2: In general:

    • to save a model instance, just invoke .save()
    • to access POST data from a form, use the cleaned_data attribute of the Form you constructed from the POST
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 284k
  • Answers 284k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you are using some dynamic pages technology (asp, php,… May 13, 2026 at 4:27 pm
  • Editorial Team
    Editorial Team added an answer The quickfix documentation in the user manual section of Vim's… May 13, 2026 at 4:27 pm
  • Editorial Team
    Editorial Team added an answer Your are deleting p at the end (root) and then… May 13, 2026 at 4:27 pm

Related Questions

I am currently developing on an advertising system, which have been running just fine
I am about to begin development of a new website and have been doing
Summary: Can I program a thick client game in C without reinventing wheels ,
I am a fairly new MySQL developer and am starting on a project that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.