Django - Tango With Django upload picture
<p>I'm on chapter 9 in Tango With Django - creating user authentication. In the registration page I have the option of uploading a picture. In my admin file everything looks good after I register myself. I show up in the User Profiles, and it even shows the image I uploaded: 
<code>Picture:     Currently: profile_images/earth.jpeg  Clear</code>. However when I click on that picture this is the error message I get:</p>

<pre><code>Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/admin/rango/userprofile/1/change/profile_images/earth.jpeg/change/
Raised by:  django.contrib.admin.options.change_view
user profile object with primary key u'1/change/profile_images/earth.jpeg' does not exist.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
</code></pre>

<p>models.py:</p>

<pre><code>from __future__ import unicode_literals

from django.db import models
from django.template.defaultfilters import slugify
from django.contrib.auth.models import User


class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)
    views = models.IntegerField(default=0)
    likes = models.IntegerField(default=0)
    slug = models.SlugField(unique=True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Category, self).save(*args, **kwargs)

    class Meta:
        verbose_name_plural = 'categories'

    def __str__(self):
        return self.name


class Page(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=128)
    url = models.URLField()
    views = models.IntegerField(default=0)

    def __str__(self):
        return self.title


class UserProfile(models.Model):
    user = models.OneToOneField(User)
    website = models.URLField(blank=True)
    picture = models.ImageField(upload_to='profile_images', blank=True)

    def __str__(self):
        return self.user.username
</code></pre>

<p>views.py - only the register():</p>

<pre><code>def register(request):
    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user

            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']
            profile.save()
            registered = True
        else:
            print user_form.errors, profile_form.errors
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    return render(request, 'rango/register.html',
                  {'user_form': user_form,
                   'profile_form': profile_form,
                   'registered': registered}
                  )
</code></pre>

<p>finally, my register.html file:</p>

<pre><code>{% extends 'rango/base.html' %}
{% load staticfiles %}

{% block title_block %}
    Register
{% endblock %}

{% block body_block %}
    &lt;h1&gt;Register with Rango&lt;/h1&gt;
    {% if registered %}
        Rango says: &lt;strong&gt;thank you for registering!&lt;/strong&gt;
        &lt;a href="/rango/"&gt;Return to the homepage&lt;/a&gt;&lt;br/&gt;
    {% else %}
        Rango says: &lt;strong&gt;register here!&lt;/strong&gt;
        Click &lt;a href="/rango/"&gt;here&lt;/a&gt; to go to the homepage&lt;br/&gt;
        &lt;form id="user_form" method="post" action="/rango/register/" enctype="multipart/form-data"&gt;
            {% csrf_token %}
            {{ user_form.as_p }}
            {{ profile_form.as_p }}

            &lt;input type="submit" name="submit" value="Register" /&gt;
        &lt;/form&gt;
    {% endif %}
{% endblock %}
</code></pre>

<blockquote>
  <p>user profile object with primary key u'1/change/profile_images/earth.jpeg' does not exist.</p>
</blockquote>

<p>It looks like one of your URL patterns may be off; it probably just wants to capture that <code>1</code> to use as the PK for a lookup, but instead is capturing <code>1/change/profile_images/earth.jpeg</code>. </p>

python django