Reputation: 165
I'm trying to show some data based on the user_id in the database but it keeps getting this ValueError. Can anyone help me find out what I am doing wrong?
This is my views.py
from django.shortcuts import render, get_object_or_404
from .models import user
def index(request):
return render(request, 'pages/index.html')
def user(request, user_id):
profile = get_object_or_404(user, pk=user_id)
context = {
'profile' : profile
}
return render(request, 'user/user.html', context)
Upvotes: 1
Views: 293
Reputation: 476729
Since you defined a function named user
, that has overwritten the reference to the user
model here.
You can use an alias in your import like:
from django.shortcuts import render, get_object_or_404
from .models import user as UserModel
def index(request):
return render(request, 'pages/index.html')
def user(request, user_id):
profile = get_object_or_404(UserModel, pk=user_id)
context = {
'profile' : profile
}
return render(request, 'user/user.html', context)
That being said, according to the PEP-8 style guidelines the names of classes should be written in CamelCase
, and that of functions in lowercase_separated_by_underscores
. So if comply to these guidelines, you should rename your user
model to User
.
Since Django has already a model named User
[Django-doc] (in django.contrib.auth
), you might want to rename to Profile
for example.
Upvotes: 1