如何在通用相册中创建视图(通用)项目 - 网址方向问题
Posted
技术标签:
【中文标题】如何在通用相册中创建视图(通用)项目 - 网址方向问题【英文标题】:How to CreateView (generic) item in generic Album - issue with url directions 【发布时间】:2017-08-21 18:47:52 【问题描述】:我对通用视图有疑问。我尝试创建表单以将照片添加到相册,但是当我通过尝试添加一些照片来测试我的应用程序时,它会将我重定向到创建相册(它假设创建照片)。为什么会出现这种情况,解决方案是什么? (也许通用不是进行此类展示的最佳方式?)一些线索?谢谢!
附言。我注意到它获得了正确的 url,但随后转到“views.DetailView.as_view(), name='detail'”而不是正确的一个“views.FotoCreate.as_view, name='foto-add'”
-这是来自详细的html:
<a href="% url 'albumbum:foto-add' album.pk %" class="btn btn-default active" role="button">
<span class="fa fa-plus fa-1x" aria-hidden="true"></span> Dodaj element do albumu</a>
网址:
urlpatterns = [
# /albumbum/
url(r'^$', views.IndexView.as_view(), name='index'),
# /albumbum/<albumbum.id>/
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
# /albumbum/add/
url(r'add/$', views.AlbumCreate.as_view(), name='add'),
# /albumbum/albumbum/2/
url(r'albumbum/(?P<pk>[0-9]+)/$', views.AlbumUpdate.as_view(), name='albumbum-update'),
# /albumbum/albumbum/2/delete
url(r'albumbum/(?P<pk>[0-9]+)/delete/$', views.AlbumDelete.as_view(), name='albumbum-delete'),
# /albumbum/2/add
url(r'^(?P<pk>[0-9]+)/add/$', views.FotoCreate.as_view, name='foto-add'),
-模型
class Album(models.Model):
autor = models.CharField(max_length=250)
nazwa_albumu = models.CharField(max_length=500)
temat = models.CharField(max_length=100)
opis_albumu = models.CharField(max_length=700, default="abc")
album_logo = models.CharField(max_length=1000)
def get_absolute_url(self):
return reverse('albumbum:detail', kwargs='pk': self.pk)
def __str__(self):
return self.nazwa_albumu + ' - ' + self.autor
class Foto(models.Model):
albumnr = models.ForeignKey(Album, on_delete=models.CASCADE)
nazwa_foto = models.CharField(max_length=250, default='abc')
obraz = models.CharField(max_length=1000, default='abc')
def get_absolute_url(self):
return reverse('albumbum:detail', kwargs='pk': self.pk)
def __str__(self):
return self.nazwa_foto
And Views.py
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from .models import Album, Foto
class IndexView(generic.ListView):
template_name = 'albumbum/index.html'
context_object_name = 'object_list'
def get_queryset(self):
return Album.objects.all()
class DetailView(generic.DetailView):
model = Album
template_name = 'albumbum/detail.html'
class AlbumCreate(CreateView):
model = Album
fields = ['autor', 'nazwa_albumu', 'temat', 'album_logo']
class AlbumUpdate(UpdateView):
model = Album
fields = ['autor', 'nazwa_albumu', 'temat', 'album_logo']
class AlbumDelete(DeleteView):
model = Album
success_url = reverse_lazy('albumbum:index')
class FotoCreate(CreateView):
model = Foto
fields = ['nazwa_foto', 'obraz']
def form_valid(self, form):
album = Foto.objects.get(pk=self.kwargs.get('pk'))
form.instance.album = album
return super(FotoCreate, self).form_valid(form)
【问题讨论】:
【参考方案1】:我想通了。我更改了一些 urls 路径,它开始正常工作。我仍然不知道为什么以前不起作用,但现在它起作用了,所以对我来说它并不重要。解决方法如下:
urlpatterns = [
# /albumbum/
url(r'^$', views.IndexView.as_view(), name='index'),
# /albumbum/<albumbum.id>/
url(r'^(?P<album_id>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
# /albumbum/add/
url(r'albumadd/$', views.AlbumCreate.as_view(), name='album-add'),
# /albumbum/albumbum/2/
url(r'albumbum/(?P<album_id>[0-9]+)/$', views.AlbumUpdate.as_view(), name='albumbum-update'),
# /albumbum/albumbum/2/delete
url(r'albumbum/(?P<album_id>[0-9]+)/delete/$', views.AlbumDelete.as_view(), name='albumbum-delete'),
# /albumbum/2/add
url(r'^(?P<album_id>[0-9]+)/fotoadd/$', views.FotoCreate.as_view(), name='foto-add'),
]
谢谢我!
【讨论】:
以上是关于如何在通用相册中创建视图(通用)项目 - 网址方向问题的主要内容,如果未能解决你的问题,请参考以下文章