python - Django: can't add image from site, but it works from Django Administration page -
i try add image form, on site. forms work trough, django administration site perfectly. on site, when press submit button, page reload, , information need add image again. informations, after reload page, stay was, added image disappear.
models.py
class urlupload(models.model): """docstring """ title = models.charfield(max_length=255) photo = models.imagefield(upload_to='/home/mariusz/web/moje/moje/static/covers/') timestamp = models.datetimefield(auto_now_add=true, auto_now=false) description = models.textfield() category = models.charfield(max_length=100) def __unicode__(self): return self.title
forms.py
from django import forms .models import urlupload class urluploadform(forms.modelform): class meta: model = urlupload fields = ['title', 'description', 'category','photo']
views.py
@login_required def profile(request): if request.method == 'post': form = urluploadform(request.post, request.files) if form.is_valid(): handle_uploaded_files(request.files['/covers/']) return httpresponseredirect('profile.html') #dodac potem strone potwierdzjaca wyslanie else: form = urluploadform() return render_to_response('profile.html', {'form': form}, requestcontext(request))
profile.html
<div class="col-md-6"> <div class="panel panel-default"> <div class="panel-heading"> <div class="panel-title"> <i class="glyphicon glyphicon-film"></i> <h4>dodaj informacje</h4> <form method="post" action="">{% csrf_token %} {{ form|crispy}} <input class="btn btn-success" type='submit' value="dodaj" /> </form> </div> </div> </div> </div>
i change views.py
@login_required def profile(request): form = urluploadform(request.post, request.files) queryset = urlupload.objects.all() if form.is_valid(): instance = form.save(commit=false) instance.save() print instance context = { "form" : form, "queryset": queryset } return render(request, "profile.html", context)
and works right now.
Comments
Post a Comment