python - How to allow users to upload a post with pictures in Django? -
this have @ moment in 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 imagine.models import document admin.site.register(document)
views.py#
from django.shortcuts import render_to_response django.template import requestcontext django.core.urlresolvers import reverse imagine.models import document 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 document list after post return httpresponseredirect(reverse('facetut.views.list')) else: form = documentform() # empty, unbound form # load documents list page documents = document.objects.all() # render list page documents , 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 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 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> {% 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 %}
any feedback great when it shows me link no picture?
i want post or blog imagefield there story , picture imagefield django can pain in butt if ask me simple hard please me
Comments
Post a Comment