每一个,我都可以从管理站点上传图片,但不知道如何从模板上传图片,
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) #image file
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
from .models import ProductsTbl
from django.template.defaultfilters import slugify
from django.contrib.auth.decorators import login_required
from django.http import Http404,HttpResponse
import datetime
def create_thing(request):
form_class = ProductsTblForm
if request.method == 'POST':
form = form_class(request.POST)
if form.is_valid():
thing = form.save(commit=False)
thing.user = request.user
thing.slug = slugify(thing.model_number)
thing.save()
return redirect('thing_detail', slug=thing.slug)
else:
form = form_class()
return render(request, 'things/create_thing.html', { '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'^create_thing/$', views.create_thing, name='create_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
class ProductsTblForm(ModelForm):
class Meta:
model = ProductsTbl
fields = ('model_number','name','feature', 'material','release','image',)
create_thing.html
{% extends 'base.html' %} {% block title %}
Create a Thing - {{ block.super }} {% endblock title %}
{% block content %}
<h1>Create a Thing</h1>
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock content %}
当我点击创建的东西时我可以看到这里的形式,但是我不知道如何让图片上传
我一直在寻找this link,但我不知道如何让它在我的应用中发挥作用,
答案 0 :(得分:1)
首先,您需要在enctype="multipart/form-data"
定义中添加<form>
。因为这是文件上传所必需的。
<form role="form" action="" method="post" enctype="multipart/form-data">
其次,您需要通过传递form_class(request.POST)
而不是request.FILES
而不是form_class(request.POST, request.FILES)
。
Dictionary<int,string>