每一个,我有一个模板,它可以编辑我的数据到数据库,但是,当我尝试从模板上传图像时,只是失败,任何人都可以告诉我如何编码和工作好的,谢谢你
models.py
from django.db import models
from django.contrib.auth.models import User
from PIL import Image
from django.utils import timezone
def get_imagep_Product(instance, filename):
return '/'.join(['products', instance.slug, filename])
class ProductsTbl(models.Model):
model_number = models.CharField(max_length=255, blank=True, null=True)
name = models.CharField(max_length=255, blank=True, null=True)
material = models.TextField(blank=True, null=True)
feature = models.TextField(blank=True, null=True)
created = models.DateTimeField(editable=False)
modified = models.DateTimeField(auto_now=True)
release = models.DateTimeField(blank=True, null=True)
submit_date = models.DateTimeField(blank=True, null=True)
slug = models.SlugField(unique=True)
user = models.ForeignKey(User, blank=True, null=True)
image = models.ImageField(upload_to=get_imagep_Product, blank=True) #try to upload this image from templates
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.created = timezone.now()
return super(ProductsTbl, self).save(*args, **kwargs)
....
views.py
from django.shortcuts import render,redirect
from .forms import ProductsTblForm,ProductsTblUploadForm
from .models import ProductsTbl,Upload
from django.template.defaultfilters import slugify
from django.contrib.auth.decorators import login_required
from django.http import Http404,HttpResponse
import datetime
....
@login_required
def edit_thing(request, slug):
# grab the object...
thing = ProductsTbl.objects.get(slug=slug)
if thing.user != request.user:
raise Http404
# set the form we're using...
form_class = ProductsTblForm
if request.method == 'POST':
# grab the data from the submitted form
form = form_class(data=request.POST,instance=thing)
if form.is_valid():
# save the new data
form.save()
return redirect('thing_detail', slug=thing.slug)
# otherwise just create the form
else:
form = form_class(instance=thing)
# and render the template
return render(request, 'things/edit_thing.html', {
'thing': thing,
'form': form,
})
....
urls.py
from django.conf.urls import patterns, url,include
from django.contrib import admin
from django.views.generic import TemplateView
from designer import views
from designer.backends import MyRegistrationView
from django.conf import settings
urlpatterns = [
....
url(r'^things/(?P<slug>[-\w]+)/edit/$', views.edit_thing, name='edit_thing'),
....
]
if settings.DEBUG:
urlpatterns += [
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
]
forms.py
from django.forms import ModelForm
from .models import ProductsTbl,Upload
class ProductsTblForm(ModelForm):
class Meta:
model = ProductsTbl
fields = ('model_number','name','feature', 'material','release','image',)
....
edit_thing.html
....
{% extends 'base.html' %} {% block title %}
Edit {{ thing.name }} - {{ block.super }} {% endblock title %}
{% block content %}
<h1>Edit "{{ thing.name }}"</h1>
<form role="form" action="" method="post" enctype="multipart/form-data" >
{% csrf_token %}
{{ form.as_p }}
<br>
<button type="submit">Submit</button>
<a href="{% url 'edit_thing_uploads' slug=thing.slug %}"> Edit images</a>
</form>
{% endblock %}
当我创建内容时,我可以像link here I asked yesterday
一样上传图片然而,当我尝试编辑表单并上传图片时失败,我不知道为什么?谢谢
答案 0 :(得分:1)
我在views.py
中解决了问题:
通过更改以下内容:
form = form_class(data=request.POST, instance=thing)
为:
form = form_class(data=request.POST, files=request.FILES, instance=thing)