1

How To Fix This Error:

int() argument must be a string, a bytes-like object or a number, not 'User'

I Want to Create User Profile I'm Tired To Fix!

I Want To Create User Page When He Register But I get this error again and again

Base.Html

  {% if user.is_authenticated %}
  <li class="nav-item">

    <a class="nav-link navaour" href="{% url 'profile' pk=user.pk %}"><i class="fa fa-user"></i>&nbsp; Profile</a>
  </li>
  <li class="nav-item">

    <a class="nav-link navaour" href="{% url 'logout' %}"><i class="fa fa-power-off"></i>&nbsp; Logout</a>
  </li>

  {% else %}
  <li class="nav-item">

    <a class="nav-link navaour" href="{% url 'register' %}"><i class="fa fa-check-square-o"></i>&nbsp; Sign up Free</a>
  </li>    
  <li class="nav-item">

    <a class="nav-link navaour" href="{% url 'login' %}"><i class="fa fa-user"></i>&nbsp; Login</a>
  </li>
  {% endif %}  

Urls.py

from . import views
from django.urls import path

urlpatterns = [
    path('', views.index,name='index'),
    path('accounts/signup/', views.user_reg,name='register'),
    path('profile/<int:pk>',views.profile_detail,name='profile')

]

Views.py

def profile_detail(request,pk):
    pk = User.objects.get(pk=pk)
    model = get_object_or_404(User, pk=pk)
    template_name = 'profile_detail_view.html'

Here is my Models.py

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse

# Create your models here.
class user_register_model(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)

    def get_absolute_url(self):
        return reverse("profile",kwargs={'pk':self.pk})

ERROR:

enter image description here

Any Help Appreciated Thanks!

4 Answers 4

1
def profile_detail(request,pk):
    pk = User.objects.get(pk=pk)
    model = get_object_or_404(User, pk=pk)
    template_name = 'profile_detail_view.html'

try to remove the following line:

pk = User.objects.get(pk=pk)
Sign up to request clarification or add additional context in comments.

6 Comments

Hi I Need To Create Url Profile it Work But I Want Also If Anyone Logout It Can Show Person Profile
@HamzaLachi are you asking to show a user profile to everyone?
yes You Are Right I'm Trying To Show Profile To Everyone
Anyway Thanks For My Help Up-voted and Answer Accepted and i up-vote your all question ad answer Again Thanks!
@HamzaLachi public profile and create or edit user's own profile should be different. The main differences 1) do not need the {% if user.is_authenticated %} 2)in ``` "{% url 'profile' pk=user.pk %}" ``` , pk could be pk=post.author.pk, something like that. Thanks a lot for up-votes!
|
0
class user_register_model(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)

    def __str__(self):
        return str(self.user.username)

    def get_absolute_url(self):
        return reverse("profile",kwargs={'pk':self.pk})

Add str function to your models

Comments

0

In the profile path you want to pass user id to the url as parameter:

path('profile/',views.profile_detail,name='profile')

But you passing the user model into it instead of the id in the template:

<a class="nav-link navaour" href="{% url 'profile' pk=user.pk %}"><i class="fa fa-user"></i>&nbsp; Profile</a>

It should be like this:

<a class="nav-link navaour" href="{% url 'profile' pk=user.id %}"><i class="fa fa-user"></i>&nbsp; Profile</a>

And you need to fix to view to convert pk string to int for query:

def profile_detail(request,pk):
    model = get_object_or_404(User, pk=int(pk))
    template_name = 'profile_detail_view.html'

4 Comments

Still Getting Same Error!
can you post full stacktrace of the error to your question ?
Question Updated Please Check!
There is your problem you passing string pk to the get object query that require int]
0

I guess this is a problem in your views.py the code User.objects.get(pk=pk), returns the User object. So your code must be

user=User.objects.get(pk=pk)

model = get_object_or_404(User, pk=user.pk)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.