sandermander
sandermander

Reputation: 73

page with unique url and users content per user

I'm trying to build a app in which every user has his own database content, but on one page/url some (chosen) database content is listed and publicly visible. I did a lot of research, but cannot come up with the logic how this works. I have a username in the URL, but when I change it to other users names, the content is not changing... What step do I miss here?

views.py

def public_view(request, username):
    if request.user.is_authenticated():
        info = Profile.objects.filter(user=request.user)
        instance = Parent.objects.filter(user=request.user)
        childs = Child.objects.filter(user=request.user)
        u = MyUser.objects.get(username=username)

        context = {
            'info': info,
            'instance': instance,
            'childs': childs,
            'u': u,
        }
        return render(request, 'public/public_home.html', context)
    else:
        raise Http404

urls.py

from django.conf.urls import url
from django.contrib import admin

from . import views


urlpatterns = [
    url(r'^user/(?P<username>\w+)/$', views.public_view, name='public_view'),
]

Upvotes: 0

Views: 97

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47354

In your view you get data for current user using request.user in querysets.You should change your queryset's argument t u object:

def public_view(request, username):
    if request.user.is_authenticated():
        u = MyUser.objects.get(username=username)
        info = Profile.objects.filter(user=u)
        instance = Parent.objects.filter(user=u)
        childs = Child.objects.filter(user=u)

        context = {
            'info': info,
            'instance': instance,
            'childs': childs,
            'u': u,
        }
        return render(request, 'public/public_home.html', context)
    else:
        raise Http404

Upvotes: 1

Related Questions