2016-09-15 16 views
0

私はDangangoで第9章にあります - ユーザー認証を作成します。登録ページには画像をアップロードするオプションがあります。私の管理ファイルでは、自分自身を登録した後、すべてがうまく見えます。ユーザープロフィールに表示され、アップロードした画像も表示されます: Picture: Currently: profile_images/earth.jpeg Clearのみレジスタ() -DjangoでDjango - Tangoアップロード画像

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 

views.py:

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} 
       ) 

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. 

models.py:私はその画像をクリックすると、しかし、これは私が取得エラーメッセージがあります最後に、私のregister.htmlファイル:

{% extends 'rango/base.html' %} 
{% load staticfiles %} 

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

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

      <input type="submit" name="submit" value="Register" /> 
     </form> 
    {% endif %} 
{% endblock %} 
+0

ちょうど推測ですが、画像へのパスが間違っているため、404'ingと思いますか? URLは最初のコードブロック 'Request URL:http:// localhost:8000/admin/rango/userprofile/1/change/profile_images/earth.jpeg/change /' – Noelkd

+0

に表示されます。私はURLのパスがそれを見つけることになるかどうかはわかりませんが。興味深いことに、自分のディレクトリに写真が入ったフォルダが作成されました。リンクをクリックしたときになぜ表示されないのか知っていますか? – Robby

+0

ユーザプロファイルページを生成するテンプレートを確認する必要があると考えますか? – Noelkd

答えて

1

主キーがu'1/change/profile_images/earth.jpegのユーザープロファイルオブジェクトが存在しません。

あなたのURLパターンの1つがオフになっているようです。ルックアップのためにPKとして使用するには1をキャプチャしたいのではなく、代わりに1/change/profile_images/earth.jpegをキャプチャしているだけです。

関連する問題