我是django的新手并试图解决这个问题,但是找不到它的错误。表单的所有验证都完美地传递,但图像实际上并未上传到媒体根目录。我试图显示图像,我只获得图像图标而不是图像。
设置根部分:
STATIC_ROOT = os.path.join(BASE_DIR, 'Vote','static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
模型代码:
class Vote(models.Model):
name = models.CharField(max_length=50)
image = models.ImageField(upload_to= "images/vote/")
rating = models.IntegerField(default=0)
vote_type = models.ForeignKey(VoteType)
def __unicode__(self):
return self.name
views.py创建(查看)代码:
def create(request):
class RequiredFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
super(RequiredFormSet, self).__init__(*args, **kwargs)
for form in self.forms:
form.empty_permitted = False
VoteFormSet = formset_factory(VoteForm, max_num=10, formset=RequiredFormSet)
if request.method == 'POST': # If the form has been submitted...
vote_list_form = VoteTypeForm(request.POST) # A form bound to the POST data
vote_list_form.pub_date = timezone.now()
# Create a formset from the submitted data
vote_item_formset = VoteFormSet(request.POST, request.FILES)
if vote_list_form.is_valid() and vote_item_formset.is_valid():
vote_list = vote_list_form.save()
for form in vote_item_formset.forms:
vote_item = form.save(commit=False)
vote_item.vote_type = vote_list
print vote_item.vote_type
vote_item.save()
return HttpResponseRedirect('created/%s' %vote_list.id) # Redirect to a 'success' page
else:
vote_list_form = VoteTypeForm()
vote_item_formset = VoteFormSet()
vote_list_form.pub_date = timezone.now()
c = {'vote_form': vote_list_form,
'vote_item_formset': vote_item_formset,
}
c.update(csrf(request))
return render_to_response('Vote/create.html', c)
还有模板代码(非常庞大,不确定是否需要):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'Vote/style.css' %}" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Dynamic Forms in Django Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// Code adapted from http://djangosnippets.org/snippets/1389/
function updateElementIndex(el, prefix, ndx) {
var id_regex = new RegExp('(' + prefix + '-\\d+-)');
var replacement = prefix + '-' + ndx + '-';
if ($(el).attr("for")) $(el).attr("for", $(el).attr("for").replace(id_regex,
replacement));
if (el.id) el.id = el.id.replace(id_regex, replacement);
if (el.name) el.name = el.name.replace(id_regex, replacement);
}
function deleteForm(btn, prefix) {
var formCount = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val());
if (formCount > 1) {
// Delete the item/form
$(btn).parents('.item').remove();
var forms = $('.item'); // Get all the forms
// Update the total number of forms (1 less than before)
$('#id_' + prefix + '-TOTAL_FORMS').val(forms.length);
var i = 0;
// Go through the forms and set their indices, names and IDs
for (formCount = forms.length; i < formCount; i++) {
$(forms.get(i)).children().children().each(function() {
updateElementIndex(this, prefix, i);
});
}
} // End if
else {
alert("You have to enter at least one todo item!");
}
return false;
}
function addForm(btn, prefix) {
var formCount = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val());
// You can only submit a maximum of 10 todo items
if (formCount < 10) {
// Clone a form (without event handlers) from the first form
var row = $(".item:first").clone(false).get(0);
// Insert it after the last form
$(row).removeAttr('id').hide().insertAfter(".item:last").slideDown(300);
// Remove the bits we don't want in the new row/form
// e.g. error messages
$(".errorlist", row).remove();
$(row).children().removeClass('error');
// Relabel/rename all the relevant bits
$(row).children().children().each(function() {
updateElementIndex(this, prefix, formCount);
if ( $(this).attr('type') == 'text' )
$(this).val('');
});
// Add an event handler for the delete item/form link
$(row).find('.delete').click(function() {
return deleteForm(this, prefix);
});
// Update the total form count
$('#id_' + prefix + '-TOTAL_FORMS').val(formCount + 1);
} // End if
else {
alert("Sorry, you can only enter a maximum of ten items.");
}
return false;
}
// Register the click event handlers
$("#add").click(function() {
return addForm(this, 'form');
});
$(".delete").click(function() {
return deleteForm(this, 'form');
});
});
</script>
</head>
<body>
<h1>Dynamic Forms in Django Example</h1>
<h2>Todo List</h2>
<form action="" method="POST" enctype="multipart/form-data">{% csrf_token %}
<div class="section">
{{ vote_form.as_p }}
</div>
<h2>Todo Items</h2>
{{ vote_item_formset.management_form }}
{% for form in vote_item_formset.forms %}
<div class="item">
{{ form.as_p }}
<p style=""><a class="delete" href="#">Delete</a></p>
</div>
{% endfor %}
<p><a id="add" href="#">Add another item</a></p>
<input type="submit" value=" Submit " />
</form>
</body>
</html>
当我使用创建视图创建文件时,它不会将图像上载到media / images / vote。为什么会这样?
编辑1:添加了一些代码 有人还告诉我,我应该将其添加到我的代码
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
现在的问题是,当我尝试获取图像时,程序会在以下位置查找图像:
"GET /Vote/216/voting/images/vote/background_OeAkutY.gif/ HTTP/1.1" 404 3135
我认为他从我的urls.py文件中获取。但我不明白为什么程序在/投票/ ID /投票...而不是MEDIA_ROOT。或者GET并不意味着它正在寻找那个地方吗?
这是我的投票/网址:
urlpatterns = [
#url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^$', views.index, name='index'),
url(r'^(?P<pk>[0-9]+)/results/$', views.results, name='results'),
url(r'^(?P<pk>[0-9]+)/voting/$', views.voting, name='voting'),
url(r'^create/$', views.create, name='create'),
url(r'^create/created/(?P<pk>[0-9]+)/$', views.created, name='created'),
]
答案 0 :(得分:0)
根据媒体文件的路径更改MEDIA_ROOT:
例如。 C:\用户\ Vato \ PycharmProjects \项目3 \投票\媒体