我有两个模型Project
,其中包含有关项目的信息,ProjectImage
包含与该项目相关的所有图像(多对一关系)。我进行了设置,以便在初始设置期间通过内联格式集上传项目的图像。
当我尝试使用图片创建新项目时,可以使用。 Firebug没有报告任何问题。但我的图片不会上传到媒体目录。据我所知,他们不会去任何地方。
我的问题:
1)。以下代码中是否有任何内容会阻止图片上传?在我看来,图片应该上传到django_project_dir/media/images
。
2)。是否有任何工具可以帮助我将来调试这些问题?
文件夹设置:
folder/
env/
project_dir/
app/
project_dir/
settings.py
media/
images/
Settings.py:
PROJECT_DIR = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
MEDIA_URL = '/media/'
Models.py:
class ProjectImage(TimeStamp):
project = models.ForeignKey(Project)
image = models.ImageField(upload_to='images/')
Views.py:
class CreateProject(CreateView):
model = Project
template_name = "manage_project.html"
form_class = CreateProjectForm
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
project_image_form = ProjectImageFormSet()
return self.render_to_response(
self.get_context_data(
form=form,
project_image_form = project_image_form,
)
)
def post(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
project_image_form = ProjectImageFormSet(self.request.POST)
if form.is_valid() and project_image_form.is_valid():
return self.form_valid(form, project_image_form)
else:
return self.form_invalid(form, project_image_form)
def form_valid(self, form, project_image_form):
self.object = form.save()
project_image_form.instance = self.object
project_image_form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form, project_image_form):
return self.render_to_response(
self.get_context_data(
form = form,
project_image_form = project_image_form,
)
)
def get_success_url(self):
return reverse('list-project')
Forms.py:
class CreateProjectForm(ModelForm):
def __init__(self, *args, **kwargs):
super(CreateProjectForm, self).__init__(*args, **kwargs)
self.fields['name'].label = "Project Name"
self.fields['description'].label = "Project Description"
self.fields['github'].label = "Github Link"
self.fields['public'].label = "Public?"
class Meta:
model = Project
ProjectImageFormSet = inlineformset_factory(Project, ProjectImage)