Commit 1caae979 authored by Brian Guadalupe's avatar Brian Guadalupe

Users can now edit their profiles

parent 0e718e46
...@@ -6,6 +6,7 @@ from django.contrib.auth import login, authenticate ...@@ -6,6 +6,7 @@ from django.contrib.auth import login, authenticate
# from django.contrib.auth.forms import UserCreationForm # from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic.edit import UpdateView
from core.forms import SignUpForm from core.forms import SignUpForm
...@@ -26,10 +27,23 @@ def signup(request): ...@@ -26,10 +27,23 @@ def signup(request):
def home(request): def home(request):
return render(request, 'home.html') return render(request, 'home.html')
def user_profile_page(request, username): def user_profile_page(request, slug):
user = get_object_or_404(User, username=username) user = get_object_or_404(User, username=slug)
return render(request, 'profile.html', {'profile': user}) return render(request, 'profile.html', {'profile': user})
# if request.user.is_authenticated:
# return render(request, 'profile.html') class EditProfile(UpdateView):
# else: model = User
# return render(request, 'profile_public.html') fields = ['username', 'email', 'first_name', 'last_name']
template_name = 'edit_profile.html'
slug_field = 'username'
slug_url_kwarg = 'slug'
# def edit_profile(request, username):
# if request.method == 'POST':
# form = EditProfile(request.POST, instance=request.user)
# if form.is_valid():
# form.save()
# return HttpResponseRedirect(reverse('update_profile_success'))
# else:
# form = EditProfile(initial={'username': request.user.username, 'email': request.user.email, 'first_name': request.user.first_name, 'last_name': request.user.last_name})
# return render(request, 'edit_profile.html', {'form': form})
\ No newline at end of file
...@@ -14,16 +14,18 @@ Including another URLconf ...@@ -14,16 +14,18 @@ Including another URLconf
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
""" """
from django.conf.urls import url from django.conf.urls import url
from django.urls import reverse_lazy
from django.contrib import admin from django.contrib import admin
from django.contrib.auth import views as auth_views from django.contrib.auth import views as auth_views
from core import views as core_views from core import views as core_views
"""import * pls"""
urlpatterns = [ urlpatterns = [
url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'), url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
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'^profile/(?P<username>[A-Za-z0-9-+_.@]+)/$', core_views.user_profile_page, name='profile'), url(r'^profile/(?P<slug>[A-Za-z0-9-+_.@]+)/$', core_views.user_profile_page, name='viewprofile'),
url(r'^$', core_views.home), url(r'^profile/(?P<slug>[A-Za-z0-9-+_.@]+)/edit/$', core_views.EditProfile.as_view(success_url=reverse_lazy('home')), name='editprofile'),
# url(r'^profile/(?P<slug>[A-Za-z0-9-+_.@]+)/edit/$', core_views.EditProfile.as_view(success_url=reverse_lazy('profile', kwargs={'update':'true'})), name='editprofile'),
url(r'^$', core_views.home, name='home'),
] ]
{% extends 'base.html' %}
{% block title %}Edit Profile{% endblock %}
{% block content %}
<div class="boxified main profile">
{% if request.user.is_authenticated %}
<h2>Edit Profile</h2>
<!-- <form method="post"> -->
<form method="post" action="{% url 'editprofile' slug=user.username %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="buttoned">Save changes</button>
</form>
{% else %}
<p>Please <a href="{% url 'login' %}">log in</a> to your account to edit your profile.</p>
{% endif %}
</div>
{% endblock %}
...@@ -4,18 +4,18 @@ ...@@ -4,18 +4,18 @@
{% block content %} {% block content %}
<div class="boxified main profile"> <div class="boxified main profile">
{% if user.is_authenticated %} {% if request.user.is_authenticated %}
<h2>{{profile.username}}'s Profile</h2> <h2>{{profile.username}}'s Profile</h2>
<ul> <ul>
<li><strong>First Name:</strong> {{profile.first_name}}</li> <li><strong>First Name:</strong> {{profile.first_name}}</li>
<li><strong>Last Name:</strong> {{profile.last_name}}</li> <li><strong>Last Name:</strong> {{profile.last_name}}</li>
<li><strong>Email Address:</strong> {{profile.email}}</li> <li><strong>Email Address:</strong> {{profile.email}}</li>
</ul> </ul>
{% if profile.username == user.username and user.is_authenticated %} {% if profile.username == request.user.username and request.user.is_authenticated %}
<a href="edit" class="buttoned">Edit user profile</a> <a href="edit" class="buttoned">Edit user profile</a>
{% endif %} {% endif %}
{% else %} {% else %}
<p>Please <a href="/login">log in</a> to your account to view {{profile.username}}'s profile.</p> <p>Please <a href="{% url 'login' %}">log in</a> to your account to view {{profile.username}}'s profile.</p>
{% endif %} {% endif %}
</div> </div>
{% endblock %} {% 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