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?
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_idis 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 theuser_iddoes not contain other non-integer characters in it):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 thatint(user_id)will not raise an issue.What about if the user is not found? user might provide
user_idbut that user id might not exist. You should handle that. The easiest way is to use django shortcutget_object_or_404like:PS: It’s not recommended to use a foreign key to a
UserwithinUserProfile. Since there is one-to-one relationship between users and their profiles, you should userOneToOneFieldinstead of aForeignKey.EDIT
If you want to be able to query users by their username instead of their id, you can do that like: