我是Django开发平台的新手。我一直在努力在Django 1.8.2(最新版本)中创建一个基本的文件上传应用程序。我找不到任何最新的示例/代码段。所以,如果您可以在此处发帖或向我推荐一个最小但完整的(模型,视图,模板)示例代码,我感谢您。
我在网上发现了很多其他的例子,但我注意到我的版本默认没有MEDIA_ROOT或MEDIA_URL属性。例如:
MEDIA_ROOT = '/path/to/myproject/media/'
MEDIA_URL = '/media/'
这是我的Models.py页面
from django.db import models
# Create your models here.
class Test(models.Model):
first_name = models.SlugField()
last_name = models.SlugField()
email = models.EmailField()
timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
updated = models.DateTimeField(auto_now_add = False, auto_now = True)
def __unicode__(self):
return self.email
这是我的Forms.py页面
from django import forms
from .models import Test
class EmailForm(forms.Form):
first_name = forms.CharField(required=False)
last_name = forms.CharField(required=False)
email = forms.EmailField()
class TestForm(forms.ModelForm):
class Meta:
model = Test
这是我的Views.py页面
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your views here.
from .forms import EmailForm, TestForm
from .models import Test
def home(request):
# print request.POST["email"], request.POST["email2"]
form = EmailForm(request.POST or None)
if form.is_valid():
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
email = form.cleaned_data['email']
new_test, created = Test.objects.get_or_create(email = email, first_name = first_name, last_name = last_name)
# # This is using Model Forms
# form = TestForm(request.POST or None)
# if form.is_valid():
# new_test = form.save(commit = False)
# #We Might Do Something Here
# email = form.cleaned_data['email']
# new_test_old, created = Test.objects.get_or_create(email = email)
# # new_test.save()
context = {"form": form}
template = "home.html"
return render(request, template, context)
这是我的URLs.py页面
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'tests.views.home', name='home'),
# url(r'^home2/$', 'lwc.views.home2', name='home'),
# url(r'^blog/', include('blog.urls')),
#This admin can be changed to anything but the
#link must also be changed to access admin area
url(r'^admin/', include(admin.site.urls)),
)
我的应用程序的目标是允许用户上传文件并提交给关联的SQLite数据库。如何在没有MEDIA_ROOT或MEDIA_URL的情况下完成此操作?