2017-06-17 6 views
0

私は自分のアカウントにプロフィール画像をアップロードする機能を提供しようとしています。問題は、私は、Djangoのユーザモデルを使用していているので、私は、私はimage = models.ImageField()Djangoは組み込みユーザーモデルを使用して画像をアップロードします

を追加することができます私自身のモデルを持っていないこれは、画像ボタンが中である私の登録ページに私のforms.py

from django import forms 
from django.contrib.auth import authenticate, get_user_model, 


class UserRegisterForm(forms.ModelForm): 
    confirm_email = forms.EmailField() 
    password = forms.CharField(widget=forms.PasswordInput) 
    confirm_password = forms.CharField(widget=forms.PasswordInput) 
    image = forms.ImageField(required=False) 

    class Meta: 
     model = User 
     fields = ['email','confirm_email','username','password'] 

    def clean_confirm_email(self): 
     email = self.cleaned_data.get("email") 
     confirm_email = self.cleaned_data.get("confirm_email") 
     if email != confirm_email: 
      raise forms.ValidationError("Emails do not match") 

     email_qs = User.objects.filter(email=email) 
     if email_qs.exists(): 
      raise forms.ValidationError("Email already used") 
     return email 

    def clean_confirm_password(self): 
     password = self.cleaned_data.get("password") 
     confirm_password = self.cleaned_data.get("confirm_password") 
     if password != confirm_password: 
      raise forms.ValidationError("Passwords do not match") 

     return password 

です私はイメージを保存し、それをユーザーにリンクする方法を知らない。私のmodels.pyとadmin.pyが空であるので、uは、私はDjangoのユーザーモデルを使用しています見ることができるように

これは、だから私のviews.py

from django.shortcuts import render, redirect 
from django.contrib.auth import (authenticate, get_user_model, login, logout) 
from .forms import UserLoginForm, UserRegisterForm 

# Create your views here. 


def login_view(request): 
    login_form = UserLoginForm(request.POST or None) 
    if login_form.is_valid(): 
     username = login_form.cleaned_data.get("username") 
     password = login_form.cleaned_data.get("password") 
     user = authenticate(username=username,password=password) 
     login(request, user) 
     return redirect('/') 
    context = { 
     "login_form": login_form 
    } 
    return render(request, 'login.html', context) 


def register_view(request): 
    form = UserRegisterForm(request.POST or None) 
    if form.is_valid(): 
     user = form.save(commit=False) 
     password = form.cleaned_data.get("password") 
     user.set_password(password) 
     user.save() 
     new_user = authenticate(username=user.username,password=password) 
     login(request, new_user) 
     return redirect('/') 
    context = { 
     'form': form, 

    } 
    return render(request, 'signup.html', context) 


def logout_view(request): 
    logout(request) 
    return redirect('/') 

です。

ほとんどのソリューションは、自分のモデルを持っていました。

答えて

0

これを後で見る人にとって、これは私がそれを解決した方法です。

私は、ユーザー

models.OneToOneField( ユーザー

を使用して

をインポートするのUserProfileモデルを作成し、django.contrib.auth.modelsから

からのユーザーと一緒にリンクされ

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

# Create your models here. 


class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    avatar = models.ImageField() # or whatever 

    def __str__(self): 
     return self.user.email 

次に、私のurl.pyをcorrecで設定するトンのURLコード

if settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

と私のsetting.py

STATIC_URL = '/static/' 
MEDIA_URL = '/media/' 

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static/'), 
) 

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') 

admin.py

from django.contrib import admin 
from .models import UserProfile 
# Register your models here. 

admin.site.register(UserProfile) 

私は、管理パネルからアップロード画像を使用することができたし、使用して私のテンプレートでそれらを表示します<img src="{{ user.userprofile.avatar.url }}">

私は十分にはっきりしていて、私は他の誰かに助けになるかもしれないことを願っています:)

0

プロファイルとしてユーザーモデルを拡張する必要があります。これを拡張するには、リンクexameを使用してください。 Add image/avatar to users in django

+0

ありがとう、私は答えのうちの1つを試してくれてありがとう、私はそれを行うことができました:) – aamurad

関連する問題