我一直试图在this turorial之后更好地理解Django中的DeleteView,一切正常,只是我的按钮标签似乎没有按照应有的方式运行。相反,我不得不使用输入标签使其工作,这使得无法在提交按钮内放置glyphicon-trash(bootstrap)。
我想知道是否与我在pythonanywhere.com上运行它有关,因为有些网站建议按钮标签在某些条件下不能正常作为<input type="submit">
的替代品。
这是我的代码。 (为了进行比较,我保留了按钮标签和输入标签。)
<ul>
{% for card in all_cards %}
<li>{{ card.front }}
<form action="{% url 'delete-card' card.id %}" method="post">
{% csrf_token %}
<input type="hidden" name="card_id" value="{{ card.id }}" >
<input type="submit" class="btn btn-default" value="DELETE">
<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-trash"></span></button>
</form>
</li>
{% endfor %}
</ul>
最让我困惑的是,当点击按钮标签时,它会给我这个错误消息。
Failed to load resource: the server responded with a /like_treasure/ status of 404 (Not Found)
* like_treasure是一个完全不同的Django项目中使用的目录的名称。
非常感谢任何建议。
url.py
from django.conf.urls import url
from django.conf import settings
from django.views.static import serve
from . import views
# import PIL
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name="index"),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^card/add/$', views.CreateCard.as_view(), name='add-card'),
url(r'^card/(?P<pk>[0-9]+)/update/$', views.UpdateCard.as_view(), name='update-card'),
url(r'^card/(?P<pk>[0-9]+)/delete/$', views.DeleteCard.as_view(), name='delete-card'),
]
if settings.DEBUG:
urlpatterns += [
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT,}),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from .models import Card
# from django.http import HttpResponseRedirect
# Create your views here.
class IndexView(generic.ListView):
template_name = "index.html"
context_object_name = "all_cards"
def get_queryset(self):
return Card.objects.all()
class DetailView(generic.DetailView):
model = Card
template_name = "detail.html"
class CreateCard(CreateView):
model = Card
fields = ['front', 'back']
class UpdateCard(UpdateView):
model = Card
fields = ['front', 'back']
class DeleteCard(DeleteView):
model = Card
success_url = reverse_lazy('index')
答案 0 :(得分:0)
只需将文件名从“url.py”更改为“urls.py”。