korakorakora
korakorakora

Reputation: 207

Where is the data created from my form going?

I just created a form for the first time and have some questions regarding the process and where the data is going.

Here are my models, views, forms, urls, and templates files;

The model from models.py:

class Member(models.Model):
    member_id = models.SlugField(max_length=10)
    name = models.CharField(max_length=200)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    mobile = models.SlugField(max_length=20)
    income = models.CharField(max_length=200, choices=INCOME_CHOICES)
    education = models.CharField(max_length=200, choices=EDUCATION_CHOICES)
    home_district = models.CharField(max_length=200, choices=DISTRICT_CHOICES)
    family_spending = models.CharField(max_length=200, choices=FAMILY_SPENDING_CHOICES)
    children_spending = models.CharField(max_length=200, choices=CHILDREN_SPENDING_CHOICES)   
    birth_date = models.DateTimeField('Birthday', blank=True)
    comments = models.CharField(max_length=300, blank=True)
    def __str__(self):
         return self.name

views.py:

def create_a_member_form(request):
  if request.method == 'POST':
    form = MemberForm(request.POST)
    if form is valid():
      member_form = form.save()
      return HttpResponseRedirect('/complete/')
  else:
    form = MemberForm()
  return render(request, 'member_form.html', {'form': form})

forms.py:

from .models import Member
from django import forms 

class MemberForm(forms.ModelForm):
    class Meta:
        model = Member
        fields = '__all__'

urls.py:

urlpatterns = [ 
    url(r'^member_form/$', views.create_a_member_form, name='member_form')
]

The template (member_form.html):

{% load staticfiles %}

<form action="/admin/" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>

I want to know:

Thank you.

Upvotes: 2

Views: 42

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599946

  1. Yes.
  2. No, it's the name you use in a {% url %} tag if you want to generate a link pointing at that URL. The template is determined by the view itself (in render(request, 'member_form.html',...)).
  3. It's not going anywhere, because your view is posting to /admin/ instead of /member_form/; /admin/ is the index of the admin site which has no code to actually accept your form data.

Note that 1 is basic HTML, and 2 and 3 are basic Django concepts which are covered in the tutorial; you should go and read that.

Upvotes: 3

Related Questions