Python URL.py中的语法错误
我试图为书签应用程序添加编辑/删除功能 我得到以下错误: My Urls.py文件如下所示:Python URL.py中的语法错误,python,django,django-forms,django-admin,django-views,Python,Django,Django Forms,Django Admin,Django Views,我试图为书签应用程序添加编辑/删除功能 我得到以下错误: My Urls.py文件如下所示: from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns('',
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myproject.views.home', name='home'),
# url(r'^myproject/', include('myproject.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'bookmarks.views.index', name='home'),
url(r'^bookmarks/$', 'bookmarks.views.index', name='bookmarks_view'),
url(r'^tags/([\w-]+)/$', 'bookmarks.views.tag'),
url(r'^login/$', 'django.contrib.auth.views.login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'})
url(r'^delete/(\d+)/$', 'bookmarks.views.delete'),
url(r'^edit/(\d+)/$', 'bookmarks.views.edit'),
)
views.py文件:
…剩余代码
def delete(request, bookmark_id):
if request.method == 'POST':
b = get_object_or_404(Bookmark, pk=int(bookmark_id))
b.delete()
return redirect(index)
def edit(request, bookmark_id):
b = get_object_or_404(Bookmark, pk=int(bookmark_id))
context = {
'form' : BookmarkForm(instance=b),
}
return render(request, 'edit.html', context)
index.html中的书签小部件
{% block bookmark_widget %}
{% if request.user %}
<div id="new-bookmark-widget">
<form method="post" action="{% url bookmarks.views.index %}">
{% csrf_token %}
<h3>Bookmark</h3>
{{ form.as_p }}
<p><button id="new-bookmark-submit">Submit</button>Submit</button>
</form>
</div>
{% endif %}
{% endblock %}
base.html中的相关div
<div id="container">
<div id="header">
{% block bookmark_widget %}
{% endblock %}
<div id="authentication">
{% if user.is_authenticated %}
Hi {{user}}! <a href="{% url 'django.contrib.auth.views.logout' %}">Logout</a>
{% else %}
<a href="{% url 'django.contrib.auth.views.login' %}">Login</a>
{% endif %}
</div>
<h1><a href="/">My bookmarking app</a></h1>
</div>
<div id="content">
<h2>{% block subheader %}{% endblock %}</h2>
{% block content %}
Sample content -- you should never see this, unless an inheriting template fails to have any content block!
{% endblock %}
</div>
<div id="footer">
All copyrights reserved
</div>
</div>
完整的bookmark.html文件:
<li>
<a class="bookmark-link" href="{{ bookmark.url }}">{% if bookmark.title %}{{ bookmark.title }}{% else %}{{ bookmark.url }}{% endif %}</a>
<div class="metadata"><span class="author">Posted by {{ bookmark.author }}</span> | <span class="timestamp">{{ bookmark.timestamp|date:"Y-m-d" }}</span>
{% if bookmark.tag_set.all %}| <span class="tags">
{% for tag in bookmark.tag_set.all %}
<a href="{% url 'bookmarks.views.tag' tag.slug %}">{{ tag.slug }}</a></span>
{% endfor %}
{% endif %}
</div>
{% if request.user.is_authenticated %}
<div class="actions"><form method=="POST" action="{% url bookmarks.view.delete bookmark.id %}>
{% csrf_token %}
<input type="submit" value="Delete">
</form> </div>
{% endif %}
我是Django的新手,如何处理错误,如何发现问题?我无法从错误消息中看到它
谢谢大家! 这行末尾缺少一个逗号:
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'})
以下行缺少逗号:
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}), <-add here
我认为您的注释部分也与您的其余代码对齐,并且在您的问题中格式不正确忘记包含错误?您真的不需要为语法错误发布这么多代码。只需将脚本的一部分贴在带有错误的行的周围。并发布错误消息。谢谢。还有一个问题:它不再在右侧显示我的表单了?语法中可能有什么错误?我在指定删除功能时是否在错误的行中添加了一些内容?我在登录右侧有一个表单,用户可以在其中添加书签。我实现的函数应该允许他们删除它们。当我添加delete函数时,标签就消失了。在我定义delete函数并通过添加新功能更改index、base和bookmarks.html之前,它工作得非常好。我的错在哪里?