如何让用户在Django上传带有图片的帖子?

时间:2016-02-06 02:26:47

标签: python django image video imagefield

这就是我目前在models.py

中所拥有的
class Document(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')
    img = models.ImageField(upload_to='documents/%Y/%m/%d', null=True, blank=True)

admin.py#

from django.contrib import admin
from imagine.models import Document

admin.site.register(Document)

views.py#

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.urlresolvers import reverse

from imagine.models import Document
from forms import DocumentForm

def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('facetut.views.list'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render_to_response(
        'list.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request))

list.html#

{% extends "base.html" %}
{% load staticfiles %}


{% block content %}

<head>
  <!-- Theme Made By www.w3schools.com - No Copyright -->
  <title>facemail</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <style>
  .jumbotron {
      background-color: #f4511e;
      color: #fff;
  }
  </style>
</head>
<body>

<div class="jumbotron text-center">
  <h1>FaceMail</h1>
  <p>who knew Email could be fun aGain</p>
</div>

{% endblock %}

{% block content2 %}



<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Minimal Django File Upload Example</title>
    </head>

    <body>
        <h2>Current Posts</h2>
        <!-- List of uploaded documents -->
        {% if documents %}
            <ul>
            {% for document in documents %}
                <li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a></li>
                  <img src="{{ document.img.url }}">
            {% endfor %}
            </ul>
        {% else %}
            <p>No documents.</p>
        {% endif %}

<br>
<br>


        <!-- Upload form. Note enctype attribute! -->
        <form action="{% url "list" %}" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            <p>{{ form.non_field_errors }}</p>
            <p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
            <p>
                {{ form.docfile.errors }}
                {{ form.docfile }}
            </p>
            <p><input type="submit" value="Upload" /></p>
        </form>

    </body>

</html>
{% endblock %}

当我这样做时,任何反馈都会很棒它只是向我显示一个没有图片的链接?

我希望它像一个帖子或博客,但有一个图像场,可能有一个故事,然后一张照片,但如果你问我这么简单但很难,那么django的图像区可能是一个痛苦的屁股请帮帮我

0 个答案:

没有答案