无法保存django编辑个人资料

时间:2017-03-31 14:21:09

标签: 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>

1 个答案:

答案 0 :(得分:0)

尝试在新表单中添加保存按钮并使用相同的POST方法。 可能会工作。