gpt4 book ai didi

python - Django:使用 form=MyForm(instance=MyID) 更新现有记录不起作用

转载 作者:行者123 更新时间:2023-11-29 23:20:48 25 4
gpt4 key购买 nike

我正在尝试更新现有记录,我已经在我调用的 View 中创建了他的表单EditRecipeForm(instance=Recipe.objects.get(id=recipe_id) 但在 View 中不出现现有字段。

这是目录结构

rsg
├── src
| ├── recipes
| | ├── forms
| | | ├── __init__.py
| | | └── update.py
| | ├── __init__.py
| | ├── admin.py
| | ├── models.py
| | ├── test.py
| | ├── urls.py
| | └── views.py
| ├── rsg
| | └── ...
| ├── signups
| | ├── ...
| | └── ...
| └── magnate.py
├── static
├── media
├── static
├── static-only
├── templates
| ├── recipes
| | ├── profileview.html
| | ├── recipedit.html
| | └── recipecreate.html
| ├── signups
| └── ....
├── ...
└── index.hml

这是配方模型:来自 rsg/src/recpes/models.py

class Recipe(models.Model):

name = models.CharField('Nome',max_length=200)
description = models.TextField('Presentazione', null=True, blank=True, default="")

directions = models.TextField('Preparazione', null=True, blank=True)
pub_date = models.DateTimeField('Data di Pubblicazione',auto_now_add=True, auto_now = False)
updated = models.DateTimeField('Data ultima modifica', auto_now_add=False, auto_now = True)
img = models.ImageField('Immagine', upload_to="/static/Images/", null=True, blank=True)

difficulty_grade = (
('bassa', 'bassa'),
('media', 'media'),
('alta', 'alta'),
('molto alta', 'molto alta'),
)

cost_size = (
('basso', 'basso'),
('medio', 'medio'),
('alto', 'alto'),
)
difficulty = models.CharField(smart_unicode('Difficoltà'), max_length=20, null=True, blank=True, choices=difficulty_grade)
time_preparation = models.IntegerField('Preparazione', null=True, blank=True)
time_preparation_min_h = models.CharField('Ore/minuti', max_length=20, null=True, blank=True,
choices=(('ore', 'h'),('minuti','min'),('giorni','gg'),))
time_cooking = models.IntegerField('Cottura', null=True, blank=True)
time_cooking_min_h = models.CharField('Ore/minuti', max_length=20, null=True, blank=True,
choices=(('ore', 'h'),('minuti','min'),('giorni','gg'),))
dose_for = models.CharField(smart_unicode('Dosi per'), max_length=20, null=True, blank=True)
cost = models.CharField(smart_unicode('Costo'), max_length=20, null=True, blank=True, choices=cost_size)

total_calories =models.DecimalField('Calorie totali', max_digits=9, decimal_places= 2, default=0)
count_like = models.IntegerField('Likes', default=0)
count_dontlike = models.IntegerField('Don\'t Likes', default=0)

# Relation with SignUp for the Author of the Recipe
author_recipe_user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='AuthorRecipeUser')

# Relation with SignUp for the Like/NotLike
voter_user = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Likes')

# Relation N:M with the Ingredients
ingredients = models.ManyToManyField(Ingredient, through='MadeWith')

def __unicode__(self):
return self.name

与其他表有一些关系,但并不重要......

包含 EditRecipeForm rsg/src/recipes/forms/update.py 的文件

from django import forms
from recipes.models import Recipe

class EditRecipeForm(forms.ModelForm):

class Meta:
model = Recipe
fields = ('name','description','directions','img','difficulty','time_preparation',
'time_preparation_min_h','time_cooking','time_cooking_min_h','dose_for',
'cost')

view.py 文件:

def recipedit(request, recipe_id):

recipe = Recipe.objects.get(pk=recipe_id)
form = EditRecipeForm(instance=recipe)

if request.POST:

if form.is_valid():
form.save()

return HttpResponseRedirect("recipes/profileview.html")

else:
form = EditRecipeForm(instance=recipe)


return render_to_response("recipes/recipedit.html",
locals(),
context_instance=RequestContext(request))

我传递了参数“instance”,但表单是空的...

我需要帮助,谢谢大家!!

<小时/>

这是模板文件

{% extends 'base.html' %}

{% block content %}
<br>
<br>
<br>
<br>
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

{% endblock %}

最佳答案

您也没有传递 POST 数据。

def recipedit(request, recipe_id):
recipe = Recipe.objects.get(pk=recipe_id)
if request.POST:
form = EditRecipeForm(request.POST, instance=recipe)
if form.is_valid():
...

关于python - Django:使用 form=MyForm(instance=MyID) 更新现有记录不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27321733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com