How can I display a user profile using Django?
22,673
- Register urls of your app in the configuration folder
project_name/urls.py
: E.g :
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^user/', include('<app_name>.urls')),
url(r'^admin/', include(admin.site.urls)),
]
- Add a new route in your
<app_name>/urls.py
. E.g :
from . import views
urlpatterns = [
url(r'profile/(?P<username>[a-zA-Z0-9]+)$', views.get_user_profile),
]
- Add a view in
<app_name>/views.py
that takeusername
(username of the user) to retrieve its information and send them into a template E.g :
from django.shortcuts import render
def get_user_profile(request, username):
user = User.objects.get(username=username)
return render(request, '<app_name>/user_profile.html', {"user":user})
- Create a template file in
<app_name>/templates/<app_name>/user_profile.html
to displayuser
object :
{{ user.username }}
{{ user.email }}
{{ user.first_name }}
{{ user.last_name }}
Replace <app_name>
by the name of your app and it should be good.
Author by
ollysmall
Updated on November 16, 2020Comments
-
ollysmall over 3 years
I am new to django and I am currently trying to build a website that allows users to sign in and view other users profiles. So far I have managed to let users sign in but I can't work out how to view other peoples profiles.
Each profile uses the users username to create a url for their profile. Currently if I sign in as one user and change the URL to another users profile URL, it still displays the current users profile. I want something similar to pinterest where any person whether they are signed in or not can view peoples profiles.
Any help would be appreciated!
View
from django.http import HttpResponse from django.shortcuts import render from howdidu.forms import UserProfileForm from howdidu.models import UserProfile from django.contrib.auth.decorators import login_required from django.shortcuts import get_object_or_404 from django.contrib.auth.models import User def index(request): context_dict = {'boldmessage': "I am bold font from the context"} return render(request, 'howdidu/index.html', context_dict) #user profile form @login_required def register_profile(request): profile = UserProfile.objects.get(user=request.user) if request.method == 'POST': form = UserProfileForm(request.POST, request.FILES, instance=profile) if form.is_valid(): form.save() return index(request) else: print form.errors else: form = UserProfileForm() return render(request, 'howdidu/register_profile.html', {'form': form}) #profile page using user name as url @login_required def profile_page(request, username): user = get_object_or_404(User, username=username) return render(request, 'howdidu/profile.html', {'profile_user': user})
project url
from django.conf.urls import patterns, include, url from django.contrib import admin from django.conf import settings from registration.backends.simple.views import RegistrationView class MyRegistrationView(RegistrationView): #redirects to home page after registration def get_success_url(self,request, user): return '/register_profile' urlpatterns = patterns('', # Examples: # url(r'^$', 'howdidu_project.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)), url(r'', include('howdidu.urls')), url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'), #redirects to home page after registration (r'^accounts/', include('registration.backends.simple.urls')), url(r'^(?P<username>\w+)/', include('howdidu.urls')), #do i need this? ) # media if settings.DEBUG: urlpatterns += patterns( 'django.views.static', (r'^media/(?P<path>.*)', 'serve', {'document_root': settings.MEDIA_ROOT}), )
app url
from django.conf.urls import patterns, url from howdidu import views urlpatterns = patterns('', url(r'^$', views.index, name='index'), url(r'^register_profile/$', views.register_profile, name='register_profile'), url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'), )
template
{% extends 'howdidu/base.html' %} {% load staticfiles %} {% block title %}{{ user.username }}{% endblock %} {% block body_block %} {% if user.is_authenticated %} <h1>{{ user.username }} welcome to your profile page</h1> <img src="{{ user.userprofile.profile_picture.url }}" width = "150" height = "150" /> <h2>{{ user.userprofile.first_name }}</h2> <h2>{{ user.userprofile.second_name }}</h2> <h2>{{ user.userprofile.user_country }}</h2> {% endif %} {% endblock %}
-
ollysmall over 8 yearsThanks for your reply! My code is near exact to that apart from different names here and there. I still seem to have the same issue. For example if my website was www.example.com and my username is oliver. When I go to www.example.com/oliver I can go to my user profile ok when signed in. Whilst still signed in if I tried to go to another users profile like www.example.com/katie, I seem to still get the user profile of oliver. What would I need to do to ensure other users data stays on each url for each users profile? Would I need to change my view in some way?
-
Louis Barranqueiro over 8 yearsin your view, you have to retrieve data of the username pass through the url
profile/(?P<username>[a-zA-Z0-9]+)$
. Add your current view to your question please -
ollysmall over 8 yearsI have just uploaded my code :-) hopefully it will make more sense now
-
Louis Barranqueiro over 8 yearsYour code is good man, it should work. Add your template too, please
-
ollysmall over 8 yearsJust uploaded the template, its very basic at the moment!
-
Louis Barranqueiro over 8 yearsYou did a mistake in your template, you send a 'profile_user' variable that contains profile data but in your template you display 'user' variable instead of 'profile_user'. Use '{{ profile_user.username }}' and it should be good.
-
ollysmall over 8 yearsThank you so so so much! That worked a treat! :-D