2017-03-31 79 views
-1

我正在嘗試使用基於功能的視圖更新配置文件模型來編輯配置文件頁面。我設法得到這個代碼前一段時間運行,但現在如果條目是新的保存按鈕只適用(沒有以前的數據是在數據庫中)無法保存django編輯配置文件

models.py

from django.db import models 
from accounts.models import User 

from django.contrib.auth.decorators import login_required 
from django.db.models.signals import post_save 

class Profile(models.Model): 
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) 
    avatar = models.ImageField(default="default_avatar.png") 
    bio = models.CharField(max_length=200, default="", blank=True) 
    skills = models.ManyToManyField("Skill") 

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

    def create_profile(sender, instance, created, **kwargs): 
     if created: 
      Profile.objects.create(user=instance) 

    post_save.connect(create_profile, sender=User) 
    # automatically run create_profile if save() method is called by user 

class Skill(models.Model): 
    name = models.CharField(max_length=50, primary_key=True) 

    def skillOwner(self): 
     return list(self.usernames.all()) 
    def __str__(self): 
     return self.name 

views.py

from django.http import HttpResponseRedirect, HttpResponse 
from django.shortcuts import render 
from django.contrib.auth.decorators import login_required 

class DetailView(generic.DetailView): 
    model = Profile 
    template_name = "profiles/user_profile.html" 

def EditProfileView(request, pk): 
    args = {'profile':Profile.objects.get(pk=pk), 'skill' : Skill.objects.all()} 

    if request.method == 'POST': 
     form = EditProfileForm(request.POST, instance=Profile.objects.get(pk=request.user)) 
     if form.is_valid(): 
      profile = form.save(commit=False) 
      profile.skills.add(Skill.objects.get(name = form.data['skill'])) 
      profile.save() 
      form.save() 
      return HttpResponseRedirect(reverse('profiles:profiles', kwargs={'pk' : request.user.pk})) 
    else: 
     form = EditProfileForm() 

    args['form'] = form 
    return render(request, 'profiles/edit_profile.html', args) 

forms.py

from django import forms 
from .models import Profile, Skill 

class EditProfileForm(forms.ModelForm): 

    avatar = forms.ImageField(required=False) 
    bio = forms.CharField(required=False) 
    skill = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(), queryset=Skill.objects.all()) 

class Meta: 
    model = Profile 
    fields = ('bio', 'skill') 

def get_context_data(self): 
    context = Skill 
    context['skills'] = Skill.objects.all() 
    return context 

edit_profile.html

  <form method="POST" enctype= multipart/form-data action="{% url 'profiles:editprofile' profile.pk %}"> 
      {% csrf_token %} 
      {% load static %} 
      <img height="100" width="100" src="{% static 'profiles/'%}{{profile.avatar}}"/><img><br> 
      {{form.avatar}}<br> 

      Bio<br> 
      {{form.bio}}<br><br> 

      Add Skills<br> 
      {{ form.skill }}<br> 
      <button type="submit" class="hol-tran-btn w-button">Save</button> 
      </form> 
+0

嘗試創建[mcve]。這裏有太多的代碼來快速發現錯誤,特別是沒有任何異常和回溯。如果某些內容不起作用,則通常會將錯誤記錄到您的服務器日誌中,或者顯示在錯誤頁面中。 –

+0

用戶是必填字段,但僅添加在post_save上? – abidibo

+0

@HåkenLid對不起,我第一次問一個問題。沒有出現錯誤頁面,但我會嘗試閱讀服務器日誌,謝謝:D – Waristea

回答

0

嘗試在新窗體中添加保存按鈕並使用相同的POST方法。 可能工作。

+0

嘗試過,但沒有奏效。它只是將表單渲染爲空 – Waristea