Reputation: 789
So I have changed settings.py so that my django app is using a PostgreSQL database. There is nothing in the database yet. What I want is, on the front page of my website, a registration form which asks for first name, last name, email, an unused username (username which doesn't already exist in my database) and age. After people submit a valid form, I want to send a confirmation email to the email address they provided. If the confirmation email is sent, I want to put in all the information which they submitted using the form into my database of registered users.
I've been google'ing around and found a lot of different ways of creating forms with django and i'm not sure what the best way for me to do it is. I first found this (https://docs.djangoproject.com/en/dev/topics/forms/) which talks about being able to email the given email address. Next, I found modelForm (https://docs.djangoproject.com/en/dev/topics/forms/modelforms/). Then, I found these two from the djangobook, which talk about signing in and signing out (http://www.djangobook.com/en/2.0/chapter14.html and https://docs.djangoproject.com/en/1.4/topics/auth/) and then I found the django-registration 1.0 documentation here (https://django-registration.readthedocs.org/en/v1.0/genindex.html#R). Which would be the best place to start for accomplishing what I want to do?
What I was trying was from the first link:
from django import forms
class ContactForm(forms.Form):
first_name = forms.CharField()
last_name = forms.CharField()
sender = forms.EmailField()
user_name = forms.CharField()
age = forms.forms.IntegerField()
and then do
from django.shortcuts import render
from django.http import HttpResponseRedirect
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
sender = form.cleaned_data['sender']
user_name = form.cleaned_data['user_name']
age = form.clean_data['age']
recipients = []
recipients.append(sender)
from django.core.mail import send_mail
send_mail(first_name, last_name, sender, recipients)
form.save()
return HttpResponseRedirect('/thanks/')
else:
form = ContactForm()
return render(request, 'contact.html', {
'form': form,
})
That's my attempt, I'm guessing it is pretty wrong since I don't even know how to edit the way these forms look using html and css and i'm not even sure what tables this will create in my database. Can anyone help me out with my task? Simply directing me to a documentation which explains how to create forms which send the information to a database (and showing how the information is sent to the database and how to access that database) will be great!
Upvotes: 2
Views: 2289
Reputation: 2942
You're not far off :) I think what you need is to understand a bit more on how all these Django parts fit together.
So, if you haven't done so already, I really recommend you go through the Django tutorial first: https://docs.djangoproject.com/en/dev/intro/tutorial01/. It might also be worthwhile rereading the docs, especially models
, forms and ModelForm
s.
(The following will make more sense once you've done the above.)
Form
s in Django are abstractions of HTML forms with some utilities that help with handling form submissions. These form submissions can then be emailed, saved to a database, ignored, or whatever you want!
Because forms often map to Django models in some way, ModelForm
s are there to help you out. For example, often you might have a form submission that gets saved to the database as a model instance. So, instead of creating a Form
that contains pretty much the same fields as an existing model, a ModelForm
helps reduce this duplication. Check out the ModelForm
example in the docs.
I don't even know how to edit the way these forms look using html and css
Check out Customizing the form template (but first make sure you understand what templates are --- do the Django tutorial mentioned above first!)
i'm not even sure what tables this will create in my database
Django will take care of most of the database details for you including saving (via a models save()
method) and creating tables (when you run python manage.py syncdb
). But again, check out the Django tutorial/docs first!
Once you've understood how all these parts fit together, then consider using django-registration as it probably does most of what you're wanting to do.
Hope this helps! :)
Upvotes: 2
Reputation: 12198
You want to read about Django Models, which can hold information and persist that information to a database.
On another note, what you want is covered by django-registration. Using that app users can sign-up, receive e-mail confirmations and information is stored in the database.
Upvotes: 0