Aus irgendeinem Grund kann ich meine Bilder nicht mit Django hochladen. Die Modelle speichern den Dateinamen, speichern die Datei jedoch in meinem Medienordner.Lade Bild mit Django
Das ist mein Modell
class Quiz(models.Model):
name = models.CharField(
verbose_name ='Quiz Title',
max_length = 50
)
description = models.TextField(
blank = True,
null = True,
verbose_name = 'Quiz Description'
)
pool = models.ForeignKey (
Pool,
verbose_name = 'Category of Quiz',
)
categories = models.ManyToManyField (
Category,
through = 'QuizCategory',
related_name = 'category',
verbose_name = 'Category',
help_text = 'Categories in this Quiz'
)
live = models.BooleanField (
verbose_name = 'Quiz is Live?',
default = False,
)
logo = models.ImageField(
upload_to = 'logos',
blank = True,
)
def __unicode__(self):
return u'%s' % (self.name)
Das ist mein GEÄNDERT istAnsicht
def create_quiz(request):
if request.method == "POST":
form = QuizForm(request.POST, request.FILES)
if form.is_valid():
# file is saved
form.save()
EDIT
Und das ist FORM Erklärung
class QuizForm (forms.ModelForm):
class Meta:
model = Quiz
fields = ('name', 'description', 'pool', 'categories', 'live', 'logo')
HTML
<form method="post" action="{% url 'create-quiz' %}" enctype="multipart/form-data" >{% csrf_token %}
<input hidden name="id" value="{% if quiz.id %}{{quiz.id}}{% else %}0{% endif %}" readonly>
<div class="col-xs-12 col-sm-8 col-md-6 col-md-offset-2">
<input required type="text" name="name" {% if quiz.name %}value="{{quiz.name}}"{% else %}placeholder="Quiz Name"{% endif %} class="form-control quiz-search-box">
<textarea required name="description" {% if quiz.description %}{% else %}placeholder="Quiz Description"{% endif %} class="form-control quiz-search-box">{% if quiz.description %}{{quiz.description}}{% endif %}</textarea>
<select name="pool" class="form-control quiz-search-box">
{% for p in pool %}<option value="{{p.name}}" {% if quiz.pool.name == p.name %}selected{% endif %}>{{p.name}}</option>{% endfor %}
</select>
<h2>Questions</h2>
<table class="table table-condenced">
{% for q in questions %}
<tr><td>{{q.name}}</td></tr>
{% endfor %}
{% if quiz != 'new' %}
<a data-toggle='modal' data-target='#myQuestionModal' class="btn btn-primary">Add Question</a>
{% endif %}
</table>
</div>
<div class="col-xs-12 col-sm-4 col-md-2 ">
<span class="number-of-questions">
Number of questions in this quiz: {{questions|length}}
</span>
<span class="add-quiz-logo">
{% if quiz.logo %}
<img id="logo" src="/media/{{quiz.logo}}" width="150" alt="Logo spacer" />
{% else %}
<img id="logo" src="{% static '/img/no-logo.png' %}" width="150" alt="Logo spacer" />
{% endif %}
<input id="id_image" type="file" class="" name="logo" onchange="readURL(this);">
</span>
<div>
<input type="submit" value="Save Quiz" class="btn btn-primary" />
</div>
</div>
</form>
Hilfe sehr willkommen. Danke
Haben Sie Kissen installiert? – PetarP
Wir sehen Ihren HTML-Code nicht, aber vergessen Sie nicht, enctype = "multipart/form-data" im
Ich hatte vergessen, aber ich habe jetzt hinzugefügt und funktioniert immer noch nicht – HenryM