中的语法错误网址.py

2024-04-24 07:12:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为书签应用程序添加编辑/删除功能。你知道吗

我得到以下错误:

我的网址.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('',
        # 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'),

)

那个视图.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)

中的书签小部件索引.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 %}

相关部门基本.html你知道吗

<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>

完整的书签.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的新手,如何处理错误,如何发现问题?我无法从错误信息中看到它。你知道吗

谢谢你!你知道吗


Tags: djangodivformidurlifadminrequest
2条回答

此行末尾缺少逗号:

    url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'})

下一行缺少逗号:

url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}), <-add here

我假设你的评论部分也与你的代码的其余部分对齐,并且在你的问题中格式不正确

相关问题 更多 >