I am new to django and tried to fix this problem, but couldn't find whats wrong with it. All the validations of forms are passed perfectly, but the image doesn't actually get uploaded to the media root. I tried to show images and I only get image icon instead of Image.
settings root part:
STATIC_ROOT = os.path.join(BASE_DIR, 'Vote','static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
models code:
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 create(view) code:
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)
and there also is template code (which is pretty huge and not sure if it is required):
<!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>
When I create a file with create view, it doesn't upload an image to the media/images/vote. Why is this happening?
EDIT 1:added some code Someone also told me that I should add this to my code
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
The Problem now is that the When I try to get Image, the program looks for images at:
"GET /Vote/216/voting/images/vote/background_OeAkutY.gif/ HTTP/1.1" 404 3135
Which I think he takes from my urls.py file. But I dont get why is the program looking in /Vote/ID/voting... instead of MEDIA_ROOT. Or does GET not mean that it is looking at that place?
This is my Vote/urls.py:
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'),
]