Есть модель:
class ProjectPage(models.Model):
“”“Stores the latest page, the page which needs to be displayed to the user.”“”
title = models.SlugField(unique = True, max_length = 100)
slug = models.SlugField(max_length = 100, unique = True)
picture = models.ImageField(blank=True, upload_to=“/static/images/download/”)
user = models.ForeignKey(User)
def save (self):
if not self.slug:
line = ‘-’.join(self.title.split())
self.slug = ‘-’.join(line.split('.'))
try:
ProjectPage.objects.get(slug = self.slug)
except ObjectDoesNotExist, e:
pass
super(ProjectPage, self).save() # Call the “real” save() method
def get_absolute_url (self):
return ‘/project/%s/’ % self.slug
def get_edit_url (self):
return ‘/project/edit/%s/’ % self.slug
def __str__ (self):
return self.title
class Admin:
pass
Также есть форма
class CreateProjectPage(forms.Form):
title = forms.CharField()
picture = forms.ImageField(required=False)
def save(self, user):
if self.cleaned_data:
page = models.ProjectPage(title = self.cleaned_data, picture = self.cleaned_data, user = user)
else:
page = models.ProjectPage(title = self.cleaned_data, user = user)
page.save()
return page
class EditProjectPage(forms.Form):
title = forms.CharField()
picture = forms.ImageField(required=False)
def __init__(self, page_ = None, *args, **kwargs):
super(forms.Form, self).__init__(*args, **kwargs)
if not page_ is None:
self.fields.initial = page_.title
if page_.picture:
self.fields.initial = page_.picture
def save (self, slug, user):
page = models.ProjectPage.objects.get(slug = slug)
page.title = self.cleaned_data
line = ‘-’.join(page.title.split())
page.slug = ‘-’.join(line.split('.'))
if self.cleaned_data:
page.picture = self.cleaned_data
page.user = user
page.save()
return page
Вьюха выглядит так
@staff_member_required
def create (request):
if request.method == ‘POST’:
create_form = forms.CreateProjectPage(request.POST)
if create_form.is_valid():
page = create_form.save(request.user)
return HttpResponseRedirect(page.get_absolute_url())
if request.method == ‘GET’:
create_form = forms.CreateProjectPage()
return render_to_response('projects/create.html', {'form':create_form})
@staff_member_required
def edit (request, slug):
if request.method == ‘POST’:
page = None
edit_form = forms.EditProjectPage(page, request.POST)
if edit_form.is_valid():
page = edit_form.save(slug, request.user)
return HttpResponseRedirect(page.get_absolute_url())
if request.method == ‘GET’:
page = models.ProjectPage.objects.get(slug = slug)
edit_form = forms.EditProjectPage(page)
return render_to_response('projects/edit.html', {'form': edit_form, ‘page’:page})
Шаблон такой
{% extends ‘base.html’ %}
{% block main %}
<div id=“content_inside_sidebar”>
</div>
<div id=“content_inside_main_edit”>
<div id=“col-one_edit”>
<div class=“boxed_edit”>
<h2 class=“title”>Create Page</h2>
<div class=“content”>
<form enctype=“multipart/form-data” id=“form1” method=“post” action=“#”>
<fieldset>
<legend>Send</legend>
<table>
{{ form.as_table }}
</table>
<input id=“inputsubmit1” type=“submit” name=“inputsubmit1” value=“Send” class=“button” />
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block contextnav %}
{% endblock %}
Но при указании рисунка для загрузки ничего на сервер не загружается. Что я делаю не так?