使用UpdateView更新图像详细信息时遇到问题。 图像详细信息已成功保存,但在更新/编辑详细信息时,表单字段显示为空。 预期的方案是在触发编辑选项时,使用现有值预先填充表单字段。
我的代码:
models.py
from photologue.models import Photo
class PhotoContent(Photo) :
user = models.ForeignKey(User)
forms.py
class SiteAccessForm(forms.Form) :
access_key = forms.CharField(required = True, label = "Website Access Key",
max_length = 10)
class PhotoForm(forms.ModelForm):
class Meta:
model = PhotoContent
fields = ('image', 'title', 'caption', 'crop_from',)
views.py
class GalleryListView(CookieMixin, FormMixin, PhotoListView) :
model = PhotoContent
form_class = SiteAccessForm
paginate_by = 20
def get_success_url(self):
username = self.kwargs['username']
return reverse("photo_list", kwargs={"username" : str(username)})
def get_template_names(self):
username = self.kwargs['username']
if Theme.objects.filter(user__username=username).exists() :
theme_selected = Theme.objects.filter(user__username=username)
template_name = 'themes/%s/photologue/photo_list.html' % (theme_selected.values()[0]['name'])
else :
template_name = 'themes/default/photologue/photo_list.html'
return [template_name]
def get_queryset(self) :
username = self.kwargs['username']
queryset = PhotoContent.objects.filter(user__username=username)
return queryset
def get_context_data(self, **kwargs):
context = super(GalleryListView, self).get_context_data(**kwargs)
logged_user = self.request.user
username = self.kwargs['username']
context['object_list_len'] = len(PhotoContent.objects.filter(user__username=username))
context['page_list'] = Page.objects.filter(user__username=username)
context['access_key'] = UserProfile.objects.filter(user__username=username).values()[0]['access_key']
context['site_url'] = Site.objects.get_current()
form_class = self.get_form_class()
form = self.get_form(form_class)
context['form'] = form
if 'access_granted' in self.request.COOKIES :
context['access_granted'] = self.request.COOKIES['access_granted']
else :
context['access_granted'] = 'False'
context['username'] = username
return context
def post(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
form_class = self.get_form_class()
form = self.get_form(form_class)
self.request.COOKIES.get('access_granted', 'False')
if form.is_valid():
user_access_key = form.cleaned_data['access_key']
username = self.kwargs['username']
access_key = UserProfile.objects.filter(user__username=username).values()[0]['access_key']
if user_access_key != access_key :
return self.form_invalid(form)
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
self.add_cookie('access_granted', 'True', max_age=3600)
return super(GalleryListView, self).form_valid(form)
def form_invalid(self, form):
error = "Incorrect Access Key provided. Try again !!"
return self.render_to_response(self.get_context_data(form=form, error=error))
def render_to_response(self, context, **response_kwargs):
response = super(GalleryListView, self).render_to_response(context, **response_kwargs)
if 'access_granted' not in self.request.COOKIES :
response.set_cookie("access_granted", "False")
return response
class PhotoUpdateView(UpdateView) :
model = PhotoContent
form_class = PhotoForm
def get_template_names(self):
if Theme.objects.filter(user=self.request.user).exists() :
theme_selected = Theme.objects.filter(user=self.request.user)
template_name = 'themes/%s/photologue/photo_form.html' % (theme_selected.values()[0]['name'])
else :
template_name = 'themes/default/photologue/photo_form.html'
return [template_name]
def get_queryset(self) :
queryset = PhotoContent.objects.filter(user=self.request.user)
return queryset
def get_context_data(self, **kwargs):
context = super(PhotoUpdateView, self).get_context_data(**kwargs)
context['username'] = self.kwargs['username']
context['logged_user'] = self.request.user
return context
def get_success_url(self):
return reverse('photo_list', kwargs={"username" : str(self.request.user)} )
photo_list.html
{% extends "themes/default/index.html" %}
{% load url from future %}
{% block content %}
<h1 class="page-header">Photo Album</h1>
{% if object_list %}
{% for photo in object_list %}
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12 thumb body_color">
<a class='href_color' href="{% url 'photo_detail' username=username pk=photo.pk %}">
<img src="{{ photo.get_thumbnail_url }}" alt="{{ photo.title }}"/><br>
{{photo.title}}
{% if logged_user == username %}
<form method="post" action="{% url 'photo_update' username=username pk=photo.pk %}">
{% csrf_token %}
<!-- When this button is clicked, it opens an empty form -->
<input type="submit" value="Edit Photo" class="btn btn-primary">
</form>
<form method="post" action="{% url 'photo_delete' username=username pk=photo.pk %}">
{% csrf_token %}
<input type="submit" value="Delete Photo" class="btn btn-primary">
</form>
{% endif %}
</a>
</div>
{% endfor %}
{% else %}
<h3>No Photos are available.</h3>
{% endif %}
{% endblock %}
photo_form.html
{% extends "themes/default/index.html" %}
{% load staticfiles %}
{% block content %}
<a href="{% url 'photo_list' username=logged_user.username %}"><button type="submit" class="btn btn-primary">Back</button></a><br>
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
<br>
{% for field in form %}
<div class='form_content_color'>
{{ field.label_tag }}<br>
{{ field }}<br><br>
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% endblock content %}
<body>
</body>
</html>
urls.py
url(r'^(?P<username>[-\w\d]+)/photo-album$', GalleryListView.as_view(), name='photo_list'),
url(r'^photo/edit/(?P<username>[-\w\d]+)/(?P<pk>[\d]+)$', auth(PhotoUpdateView.as_view()), name='photo_update'),
非常感谢任何帮助/建议。
由于
答案 0 :(得分:2)
解决了!!
我犯了一个使用&#39; POST&#39;编辑链接中的方法。
<form method='post' action="{% url 'photo_update' username=username pk=photo.pk %}">
{% csrf_token %}
<input type="submit" value="Edit Photo" class="btn btn-primary">
</form>
更改为
<form action="{% url 'photo_update' username=username pk=photo.pk %}">
{% csrf_token %}
<input type="submit" value="Edit Photo" class="btn btn-primary">
</form>