2

私はこれに別の夜を費やしてきました...誰かが助けることができますか?

私はhttp://lightbird.net/dbe/todo_list.htmlでキックスタートの例を複製しようとしています。これは以前のバージョンのDjango用に開発されたようです(ハウツーを包括的に説明していません)

とにかく、「タスクを完了としてマークする」までタスクを管理します。そして、私の問題が始まります。

明確にするために、ItemAdminクラスのlist_display属性に「mark_done」を追加するまではすべて問題ありません。次に、truc.todo.views.mark_doneを解決できないようです。完全な絶対モジュール参照と、以下に示す部分参照を試しましたが、役に立ちませんでした。他のプロジェクトへの移植が容易であることを意味するので、部分的なもの(「todo」で始まる)を好みます。

よろしくお願いします。

私は次のようなファイルツリーを持っています:

truc/ (my project)
|-todo/ (my app)
| |-__init__.py
| |-views.py
| |-urls.py  (module urls)
| |-models.py
| |-templates/admin/todo/item/change_list.html
|-truc/
| |-urls.py  (main urls)
| |-settings.py
| |-etc.

私の主なURLには次のものが含まれています。

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^todo/$', include('todo.urls')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

私のモジュールのURLには次のものが含まれています。

from django.conf.urls import patterns, include, url

urlpatterns = patterns('todo.views',
    url(r'^mark_done/(\d*)/$', 'mark_done'),
)

models.pyに私は持っています:

from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin

from django.utils.translation import ugettext as _
from django.utils.encoding import force_text
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse

[...]
class Item(models.Model):
    name = models.CharField(max_length=60)
    created = models.ForeignKey(DateTime)
    priority = models.IntegerField(default=0)
    difficulty = models.IntegerField(default=0)
    done = models.BooleanField(default=False)

    def mark_done(self):
### The below is THE line which I cannot have work...
        return "<a href='%s'>Done</a>" % reverse("todo.views.mark_done", args=[self.pk])
    mark_done.allow_tags = True

class ItemAdmin(admin.ModelAdmin):
    list_display = ["name", "priority", "difficulty", "created", "mark_done", "done"]
    search_fields = ["name"]
[...]

エラーとトレースバック:

NoReverseMatch at /admin/todo/item/
Reverse for 'todo.views.mark_done' with arguments '(5,)' and keyword arguments '{}' not found.
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/todo/item/
Django Version: 1.5
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'todo.views.mark_done' with arguments '(5,)' and keyword arguments '{}' not found.
Exception Location: /Library/Python/2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 416
Python Executable:  /usr/bin/python
Python Version: 2.7.1

[...]
Template error:
In template /Users/arnaud/Documents/truc/todo/templates/admin/todo/item/change_list.html, error at line 92
   Reverse for 'todo.views.mark_done' with arguments '(5,)' and keyword arguments '{}' not found.
   82 :         {% endif %}


   83 :       {% endblock %}


   84 : 


   85 :       <form id="changelist-form" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}


   86 :       {% if cl.formset %}


   87 :         <div>{{ cl.formset.management_form }}</div>


   88 :       {% endif %}


   89 : 


   90 :       {% block result_list %}


   91 :           {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}


   92 :            {% result_list cl %} 


   93 :           {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}


   94 :       {% endblock %}


   95 :       {% block pagination %}{% pagination cl %}{% endblock %}


   96 :       </form>


   97 :     </div>


   98 :   </div>


   99 : {% endblock %}


   100 : 

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  140.                     response = response.render()
File "/Library/Python/2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/Library/Python/2.7/site-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  124.         return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  124.         return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  63.             result = block.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  63.             result = block.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  1185.                     _dict = func(*resolved_args, **resolved_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
  286.             'results': list(results(cl))}
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in results
  264.             yield ResultList(None, items_for_result(cl, res, None))
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in __init__
  256.         super(ResultList, self).__init__(*items)
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in items_for_result
  184.             f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "/Library/Python/2.7/site-packages/django/contrib/admin/util.py" in lookup_field
  258.                 value = attr()
File "/Users/arnaud/Documents/truc/todo/models.py" in mark_done
  76.         return "<a href='%s'>Done</a>" % reverse("todo.views.mark_done", args=[self.pk])
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse
  496.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  416.                 "arguments '%s' not found." % (lookup_view_s, args, kwargs))

Exception Type: NoReverseMatch at /admin/todo/item/
Exception Value: Reverse for 'todo.views.mark_done' with arguments '(5,)' and keyword arguments '{}' not found.

urlresolvers.pyの_reverse_with_prefixのようなローカル変数:

candidate   u'/todo/mark_done/5/'
val 5
pattern 'todo/$mark_done/(\\d*)/$'
self    <RegexURLResolver 'truc.urls' (None:None) ^/>
args    (5,)
_prefix u'/'
m   'todo.views'
possibility [(u'todo/mark_done/%(_0)s/', [u'_0'])]
lookup_view_s   u'todo.views.mark_done'
possibilities   [([(u'todo/mark_done/%(_0)s/', [u'_0'])], 'todo/$mark_done/(\\d*)/$', {})]
lookup_view <function mark_done at 0x10da242a8>
prefix_norm u'/'
prefix_args []
params  [u'_0']
result  u'todo/mark_done/%(_0)s/'
defaults    {}
kwargs  {}
n   'mark_done'
unicode_args    [u'5']
4

2 に答える 2

6

この行:

url(r'^todo/$', include('todo.urls'))

ターミネータ記号は必要ありません$。インクルードが機能しなくなります。私は偶然これをいつもやっています。

于 2013-03-15T00:45:59.580 に答える
2

次のように URL パターンの名前を指定する必要があります。

urlpatterns = patterns('todo.views',
    url(r'^mark_done/(\d+)/$', 'mark_done', name='mark_done'),
)

$停止の原因となっている「todo」パターンから終了パターンを削除します。

urlpatterns = patterns('',
    (r'^todo/', include('todo.urls')),
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^admin/', include(admin.site.urls)),
)

次に、リバースを使用して、名前と引数を渡すことができます。

def mark_done(self):
    return "<a href='{0}'>Done</a>".format(reverse("mark_done", args=(self.pk,)))
mark_done.allow_tags = True

mark_doneコードサンプルに表示されていないどこかで呼び出されたビュー関数があると仮定しています。

于 2013-03-15T00:47:50.577 に答える