将“照片”表单添加到我的表单,以允许用户添加照片

时间:2012-11-06 22:48:29

标签: django django-forms django-models

我想将Imageform添加到我的表单中,以允许所有用户添加照片。 我读过这个https://docs.djangoproject.com/en/dev/ref/forms/fields/#imagefield 这个https://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-files

但我仍然对如何实现这一点感到困惑。如何更改我的代码以允许用户从他们自己的文件夹添加照片?这是我的代码,我还附上了我想要创建的理想表单。

[我的理想形式]

My Ideal Form

models.py

from django.db import models
from django.forms import ModelForm

class Sell(models.Model):
    subject = models.CharField(max_length=100)
    price = models.CharField(max_length=100)
    condition = models.CharField(max_length=100)
    photo = models.ImageField(upload_to="media")
    email = models.EmailField()
    body = models.CharField(max_length=200)

forms.py

from django.forms import ModelForm
from uasite1.models import Sell

class SellForm(ModelForm):
    class Meta:
        model = Sell

views.py

from django.shortcuts import render_to_response,get_object_or_404
from django.http import HttpResponseRedirect
from uasite1.forms import SellForm
from uasite1.models import Sell
from django.template import RequestContext

def sell_create(request):
    context = {}
    if request.method == 'POST':
        form = SellForm(request.POST)
        if form.is_valid():            
            new_sell = form.save()
            return HttpResponseRedirect('/sechand/%d/' % new_sell.pk)
    else:
        form = SellForm()
    context['form'] = form
    return render_to_response('sell.html',context,context_instance = RequestContext(request))

sell.html

{% extends 'base.html' %}
{% block extrahead %}
{% endblock %}
{% block content %}

<form enctype="multipart/form-data" action = "/sechand/" method = "post">
    {% csrf_token %}
    {{ form.as_p }}

<input type = "submit" value="Submit" />
</form>

{% endblock%}

sell_display.html(这是提交信息显示的模板。)

{% extends 'base.html' %}
{% block content %}
<div id = 'sell'>

    <h3> Product Name : [ {{ sell.subject }}]</h3>
    <p>Photo{{ sell.photo }}</p>
    <p>Price: [ {{ sell.price }} ]</p>
    <p>Condition: [ {{ sell.condition }} ]</p>
    <p>Comments: [ {{sell.body}} ]</p>
    <p>Contact Email:[ {{ sell.email }} ]</p>
</div>
{% endblock %}

urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^sechand/$','uasite1.views.sell_create'),
    url(r'^sechand/(?P<pk>\d+)/$', 'uasite1.views.sell_detail'),
    url(r'^products/electronics/$', 'uasite1.views.Electronics'),
    url(r'^products/$', 'uasite1.views.ProductsAll'),
    url(r'^admin/', include(admin.site.urls)),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

1 个答案:

答案 0 :(得分:0)

我没有查看整个代码,但我注意到一个重要的错误,视图应该包含以下内容:

form = SellForm(request.POST, request.FILES)

此外,这一行:

<p>Photo{{ sell.photo }}</p>

我不认为这是展示照片的方式,我会给照片添加链接:

<p>Photo{{ sell.photo.url }}</p>

最后,对于开发环境,您可能需要提供媒体文件,因此只需将此行添加到主urls.py文件中:

from django.conf.urls.static import static
from django.conf import settings

urlpatterns = patterns('',
# your url patterns in here

)+ static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)

编辑:

Serving media files。这个链接应该给你一个很好的概述,这让我很困惑,所以我会给你一个例子,就像我在我的项目中一样。首先我在settings.py中执行此操作:

#settings.py
from os.path import dirname, join, realpath
# have in mind that I have settings.py under project_root/project/settings.py
# so you probably want to check your path to your project root folder first
ROOT_DIR = realpath(join(dirname(__file__), '..'))
MEDIA_ROOT = realpath(join(ROOT_DIR, 'media'))
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    join(ROOT_DIR, "static"),
)

这就是我这样做的方式,你可以找到很多方法也可以。为了给你一个更好的线索我的文件夹看起来像这样:

project_root/
    media/
    static/
    project/
        settings.py