forked from django/djangoproject.com
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a simple user profile edit form and view.
- Loading branch information
Showing
9 changed files
with
185 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,35 @@ | ||
from __future__ import absolute_import | ||
from django import forms | ||
from registration.forms import RegistrationFormUniqueEmail | ||
from .models import Profile | ||
|
||
class RegistrationForm(RegistrationFormUniqueEmail): | ||
def __init__(self, *args, **kwargs): | ||
super(RegistrationForm, self).__init__(*args, **kwargs) | ||
del self.fields["tos"] | ||
del self.fields["tos"] | ||
|
||
class ProfileForm(forms.ModelForm): | ||
""" | ||
A form for editing user profiles. | ||
Assumes that the Profile instance passed in has an associated User | ||
object. The view (see views.py) takes care of tha | ||
""" | ||
class Meta(object): | ||
model = Profile | ||
fields = ['name'] | ||
email = forms.EmailField(required=False) | ||
|
||
def __init__(self, *args, **kwargs): | ||
instance = kwargs.get('instance', None) | ||
if instance: | ||
kwargs.setdefault('initial', {}).update({'email': instance.user.email}) | ||
super(ProfileForm, self).__init__(*args, **kwargs) | ||
|
||
def save(self, commit=True): | ||
instance = super(ProfileForm, self).save(commit=commit) | ||
if 'email' in self.cleaned_data: | ||
instance.user.email = self.cleaned_data['email'] | ||
if commit: | ||
instance.user.save() | ||
return instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# encoding: utf-8 | ||
import datetime | ||
from south.db import db | ||
from south.v2 import SchemaMigration | ||
from django.db import models | ||
|
||
class Migration(SchemaMigration): | ||
|
||
def forwards(self, orm): | ||
|
||
# Adding model 'Profile' | ||
db.create_table('accounts_profile', ( | ||
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), | ||
('user', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['auth.User'], unique=True)), | ||
('name', self.gf('django.db.models.fields.CharField')(max_length=200, blank=True)), | ||
)) | ||
db.send_create_signal('accounts', ['Profile']) | ||
|
||
|
||
def backwards(self, orm): | ||
|
||
# Deleting model 'Profile' | ||
db.delete_table('accounts_profile') | ||
|
||
|
||
models = { | ||
'accounts.profile': { | ||
'Meta': {'object_name': 'Profile'}, | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'name': ('django.db.models.fields.CharField', [], {'max_length': '200', 'blank': 'True'}), | ||
'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'}) | ||
}, | ||
'auth.group': { | ||
'Meta': {'object_name': 'Group'}, | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), | ||
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) | ||
}, | ||
'auth.permission': { | ||
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, | ||
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), | ||
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) | ||
}, | ||
'auth.user': { | ||
'Meta': {'object_name': 'User'}, | ||
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), | ||
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), | ||
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), | ||
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), | ||
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), | ||
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), | ||
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), | ||
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), | ||
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), | ||
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), | ||
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) | ||
}, | ||
'contenttypes.contenttype': { | ||
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, | ||
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), | ||
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) | ||
} | ||
} | ||
|
||
complete_apps = ['accounts'] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.db import models | ||
from django.contrib.auth.models import User | ||
|
||
class Profile(models.Model): | ||
user = models.OneToOneField(User) | ||
name = models.CharField(max_length=200, blank=True) | ||
|
||
def __unicode__(self): | ||
return self.name or unicode(self.user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{% extends "registration/base.html" %} | ||
|
||
{% block title %}Edit your profile{% endblock %} | ||
|
||
{% block content %} | ||
|
||
{% if form.errors %} | ||
<p class="errors">Please correct the errors below: {{ form.non_field_errors }}</p> | ||
{% endif %} | ||
|
||
<h1>Edit your profile</h1> | ||
|
||
<form method="post" action="" class="wide"> | ||
{% csrf_token %} | ||
<p> | ||
<label for="id_name">Your name:</label> | ||
{% if form.name.errors %} | ||
<p class="errors">{{ form.name.errors.as_text }}</p> | ||
{% endif %} | ||
{{ form.name }} | ||
</p> | ||
<p> | ||
<label for="id_email">Your email address:</label> | ||
{% if form.email.errors %} | ||
<p class="errors">{{ form.email.errors.as_text }}</p> | ||
{% endif %} | ||
{{ form.email }} | ||
</p> | ||
<p class="submit"><input type="submit" value="Save →"></p> | ||
{% endblock %} | ||
|
||
{% block content-related %} | ||
<h2>Help</h2> | ||
<p>Use this form to edit your profile.</p> | ||
|
||
<p>Use whatever name you'd like to be identified with on djangoproject.com. If | ||
you leave it blank, we'll identify you as <b>{{ user.username }}</b>, your | ||
username.</p> | ||
|
||
<p>We hate spam as much as you do. We'll only use it to send you password reset | ||
emails. We'll also use this email to try to fetch a <a | ||
href="http://en.gravatar.com/">Gravatar</a>. You can change the image for this | ||
email at <a href="http://en.gravatar.com/">Gravatar</a>.</p> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters