Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:15:36+00:00 2026-06-12T00:15:36+00:00

I am trying to select a certain user based on a user_id but am

  • 0

I am trying to select a certain user based on a user_id but am having some trouble. I’m getting an error saying "invalid literal for int() with base 10":

The relevant parts of the code are below:

urls.py

url(r'^user/(?P<user_id>[-\w]+)/$', 'view_reviews_user'),

models.py

class UserProfile(models.Model):
    user = models.ForeignKey(User, primary_key=True)
    quote = models.CharField('About', max_length =  200, null=True, blank=True)

views.py

def view_reviews_user(request, user_id):
    reviews = Reviewbackup.objects.filter(user=user_id)
    reviews = reviews.order_by("-created_on")
    userprofile = UserProfile.objects.get(user=user_id)
    return render_to_response('reserve/templates/user.html', {'reviews':reviews, 'user_id': user_id, 'userprofile': userprofile},
        context_instance=RequestContext(request))

I believe the error is in the "pk=user_id" but am not sure how to fix it. I tried "user_id=user_id" but that does’t seem to work either. I am trying to access "quote" in the template eventually. Any advice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:15:37+00:00Added an answer on June 12, 2026 at 12:15 am

    You have to remember what is going on before the view gets called.

    Django does regex to match the url with some pattern. Now usually regex expressions will have groups in them to be able to parse out some ids from there. Django will take those ids and pass them to the view. However since it just uses regex, the values of those groups will be strings even if your groups are something like (?P<user_id>\d+).

    So within your view, user_id is a string, whereas foreign key in your model is an integer, hence Django wants an integer to do the query. All you have to do is cast the value to an integer and you should be fine (refer below on how to make sure the user_id does not contain other non-integer characters in it):

    def view_reviews_user(request, user_id):
        userprofile = UserProfile.objects.get(pk=int(user_id))
    

    Even though that will work, many things can go wrong here.

    What about if the user does not type an integer? That is easily fixed in the regex in urlconfig by using \d+ which will only match the expression if there are only integers. So then you can be sure that int(user_id) will not raise an issue.

    url(r'^user/(?P<user_id>\d+)/$', 'view_reviews_user'),
    

    What about if the user is not found? user might provide user_id but that user id might not exist. You should handle that. The easiest way is to use django shortcut get_object_or_404 like:

    get_object_or_404(UserProfile, pk=user_id)
    

    PS: It’s not recommended to use a foreign key to a User within UserProfile. Since there is one-to-one relationship between users and their profiles, you should user OneToOneField instead of a ForeignKey.

    EDIT

    If you want to be able to query users by their username instead of their id, you can do that like:

    def view_reviews_user(request, username):
        userprofile = get_object_or_404(UserProfile, user__username=username)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use css3's :not() selector to select certain elements that do
I'm trying to select all dom elements that have id=mydiv but exclude the ones
I am trying to find out how to allow users to only select certain
I have an Oracle database where I'm trying to select a user field from
I'm trying to create a filter in ASP.net, where a user can select a
I am trying to select a certain value from a multi-element background-image property such
I am trying to select questions which have a certain topic id. These matches
am trying to allow a user to delete a certain image with is in
I'm trying to determine whether or not the user has voted on a certain
Im trying to build a page that will allow a user to select a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.