2017-06-17 69 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 

在我的註冊頁面的圖像按鈕被顯示,我不知道如何保存圖像並將其鏈接到用戶。

這是我的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('/') 

所以就像你看到的,我使用的Django用戶模型,所以我的models.py和admin.py是空的。

他們大部分的解決方案都是關於擁有自己的模型。

回答

0

所以對於那些稍後可能會看到這些的人來說,這就是我解決這個問題的方法。

我創建了一個用戶配置模型,並與用戶連接它從

使用models.OneToOneField 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

您需要將用戶模型擴展爲配置文件。使用下面的鏈接來擴展這個。 Add image/avatar to users in django

+0

非常感謝你我嘗試了其中一個提供的答案,很少有研究我能夠做到這一點:) – aamurad