Как редактировать подписку на django moellform

новичок в django. Я создаю форму для сохранения пользователем в базе данных. часть сохранения в базе данных работает. Однако я хочу иметь возможность просматривать и редактировать данные в двух отдельных шаблонах. назовем их view_self_eval.html и edit_self_eval.html. Я застрял.

Ниже приведен мой код для Models.py, Forms.py и View.py для конкретной формы.

Models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.forms import ModelForm

class self_eval(models.Model):
    Owner = models.OneToOneField(User, on_delete=models.CASCADE, default=None)
    First_Name = models.CharField(max_length=100, default=None)
    Last_Name = models.CharField(max_length=100, default=None)
    Current_Role = models.CharField(max_length=100)
    My_Strengths = models.CharField(max_length=1000)
    Improvement = models.CharField(max_length=4000)
    Goals = models.CharField(max_length=4000)
    Profesional_Certification = models.CharField(max_length=4000)
    Important_discussion = models.CharField(max_length=4000)


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

def create_self_eval(sender, **kwargs):
    if kwargs['created']:
        self_eval = self_eval.objects.create(user=kwargs['instance'])
post_save.connect(create_self_eval, sender=User)

Forms.Py

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from accounts.models import self_eval

class SelfEvalForm(forms.ModelForm):
    class Meta:
        model = self_eval
        fields=(
            'First_Name',
            'Last_Name',
            'Current_Role',
            'My_Strengths',
            'Improvement',
            'Goals',
            'Profesional_Certification',
            'Important_discussion'
        )
    def save(self, commit=True):
        self_eval = super(SelfEvalForm, self).save(commit=False)
        self_eval.First_Name = self.cleaned_data['First_Name']
        self_eval.Last_Name = self.cleaned_data['Last_Name']
        self_eval.Current_Role = self.cleaned_data['Current_Role']
        self_eval.My_Strengths = self.cleaned_data['My_Strengths']
        self_eval.Improvement = self.cleaned_data['Improvement']
        self_eval.Goals = self.cleaned_data['Goals']
        self_eval.Profesional_Certification = self.cleaned_data['Profesional_Certification']
        self_eval.Important_discussion = self.cleaned_data['Important_discussion']

        if commit:
            self_eval.save()

        return self_eval

Views.py

from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from accounts.forms import (
    RegistrationForm,
    EditProfileForm,
    SelfEvalForm,
    EditSelfEvalForm
)
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm, PasswordChangeForm
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.decorators import login_required
from .models import self_eval
from django.views.generic import UpdateView
def self_eval(request):
    if request.method == 'POST':
        form = SelfEvalForm(request.POST)
        if form.is_valid():
            #save form data to DB
            instance = form.save(commit=False)
            instance.Owner=request.user
            instance.save()
            return redirect(reverse('accounts:self_eval'))

    else:
        form = SelfEvalForm()
        args = {'form': form}
        return render(request, 'accounts/self_eval_form.html', args)

def edit_self_eval(request, Owner_id):
    self_eval = get_object_or_404(self_eval, pk=Owner_id)
    if request.method == 'POST':
        form = SelfEvalForm(request.POST, instance=Current_Role, Owner=request.user)
        if form.is_valid():
            self_eval = form.save()
    else:
        form = SelfEvalForm(intance=self_eval, Owner=request.user)
    args = {'form':form,
            'Current_Role':Current_Role
    }
    return render(request, 'accounts/edit_self_eval.html', args)

Я вижу опечатку: request.POSt должен быть request.POST

HP Bruna 13.09.2018 21:28

исправлю это. Благодарность

liucyfer 13.09.2018 21:34
0
2
16
0

Другие вопросы по теме