python - Django: Update exsisting record with form=MyForm(instance=MyID) doesn't work -
i'm try update existing record, had create form, in view call editrecipeform(instance=recipe.objects.get(id=recipe_id) in view don't appear existing fields.
here structure of directory
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
here recipe models: 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 signup author of recipe author_recipe_user = models.foreignkey(settings.auth_user_model, related_name='authorrecipeuser') # relation signup like/notlike voter_user = models.manytomanyfield(settings.auth_user_model, through='likes') # relation n:m ingredients ingredients = models.manytomanyfield(ingredient, through='madewith') def __unicode__(self): return self.name
there relation other table it's not important...
the file contains editrecipeform rsg/src/recipes/forms/update.py
from django import forms 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')
the view.py file:
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))
i pass argument "instance" form empty...
i need thank everybody!!
this template file
{% 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 %}
you didn't pass post data well.
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(): ...
Comments
Post a Comment