Commit 7f6fdf8f authored by Brian Guadalupe's avatar Brian Guadalupe

Implemented user profiles

parent 9dd73b13
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=255)
last_name = forms.CharField(max_length=255)
email = forms.EmailField(max_length=255, help_text='Please enter a valid email address.')
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )
from django.db import models from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here. # Create your models here.
# class Profile(models.Model):
# user = models.OneToOneField(User, on_delete=models.CASCADE)
# first_name = models.TextField(max_length=255)
# last_name = models.TextField(max_length=255)
# email_address = models.EmailField(max_length=255)
@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
...@@ -3,22 +3,33 @@ from django.shortcuts import render ...@@ -3,22 +3,33 @@ from django.shortcuts import render
# Create your views here. # Create your views here.
from django.contrib.auth import login, authenticate from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm # from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect from django.contrib.auth.models import User
from django.shortcuts import render, redirect, get_object_or_404
from core.forms import SignUpForm
def signup(request): def signup(request):
if request.method == 'POST': if request.method == 'POST':
form = UserCreationForm(request.POST) form = SignUpForm(request.POST)
if form.is_valid(): if form.is_valid():
form.save() form.save()
username = form.cleaned_data.get('username') username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1') raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password) user = authenticate(username=username, password=raw_password)
login(request, user) login(request, user)
return redirect('/home/') return redirect('/')
else: else:
form = UserCreationForm() form = SignUpForm()
return render(request, 'signup.html', {'form': form}) return render(request, 'signup.html', {'form': form})
def home(request): def home(request):
return render(request, 'home.html') return render(request, 'home.html')
def user_profile_page(request, username):
user = get_object_or_404(User, username=username)
return render(request, 'profile.html', {'profile': user})
# if request.user.is_authenticated:
# return render(request, 'profile.html')
# else:
# return render(request, 'profile_public.html')
No preview for this file type
...@@ -120,4 +120,4 @@ USE_TZ = True ...@@ -120,4 +120,4 @@ USE_TZ = True
STATIC_URL = '/static/' STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/home/' LOGIN_REDIRECT_URL = '/'
...@@ -23,5 +23,6 @@ urlpatterns = [ ...@@ -23,5 +23,6 @@ urlpatterns = [
url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'), url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),
url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls),
url(r'^signup/', core_views.signup, name='signup'), url(r'^signup/', core_views.signup, name='signup'),
url(r'^home/', core_views.home), url(r'^profile/(?P<username>[A-Za-z0-9-+_.@]+)/$', core_views.user_profile_page, name='profile'),
url(r'^$', core_views.home),
] ]
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
</head> </head>
<body> <body>
<header> <header>
<h1>MyMusicList Login</h1> <h1>MyMusicList</h1>
{% if user.is_authenticated %} {% if user.is_authenticated %}
Hi {{ user.username }}! Hi {{ user.username }}!
<a href="{% url 'logout' %}">logout</a> <a href="{% url 'logout' %}">logout</a>
......
...@@ -4,4 +4,5 @@ ...@@ -4,4 +4,5 @@
{% block content %} {% block content %}
<h2>Home</h2> <h2>Home</h2>
<a href="/profile/{{user.username}}">View your profile</a>
{% endblock %} {% endblock %}
{% extends 'base.html' %}
{% block title %}{{profile.username}}'s Profile{% endblock %}
{% block content %}
<h2>{{profile.username}}'s Profile</h2>
<ul>
<li><strong>First Name:</strong> {{profile.first_name}}</li>
<li><strong>Last Name:</strong> {{profile.last_name}}</li>
<li><strong>Email Address:</strong> {{profile.email}}</li>
</ul>
{% if profile.username == user.username and user.is_authenticated %}
<a href="#">Edit user profile</a>
{% endif %}
{% endblock %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment