无法导入' edit_contact'。路径必须完全合格

时间:2016-11-03 15:20:03

标签: python django

所以,我试图在Python / Django中创建一个充满联系人的表。当我尝试运行该程序时,收到上述错误消息(" ImportError:无法导入' edit_contact'。路径必须完全合格。")

以下是我使用的views.py:

from contacts.models import Contact
#, Address, Telephone
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404, render
from django.template import Context, loader
from django.forms.models import inlineformset_factory
from django.template import loader, Context, RequestContext
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
# Create your views here.

def index(request):
    #return HttpResponse("Hey! You can see contacts here!")
    contact_list = Contact.objects.all().order_by('last_name')
    return render_to_response('contacts/index.html', {'contact_list': contact_list},
            RequestContext(request))

def detail(request, contact_id):
    c = get_object_or_404(Contact, pk=contact_id);

def new_contact(request):
    print "new_contact"

    #AddressInlineFormSet = inlineformset_factory(Contact,

    if request.method == "POST":
            form = ContactForm(request.POST)
            if form.is_valid():
                    contact = form.save()
            return HttpResponseRedirect(reverse('contacts.views.detail', args=(contact.pk,)))
    else:
            form = ContactForm()

    return render_to_response("contacts/form.html",{
            "form": form,
            }, RequestContext(request))

def edit_contact(request, contact_id):
    contact = Contact.objects.get(pk=contact_id)
    if request.method == "POST":
            form = ContactForm(request.POST, instane=contact)
            if form.is_valid():
                    form.save()
                    return HttpResponseRedirect(reverse('contacts.views.detail', args=(contact.pk,)))
    else:
            form = ContactForm(instance = contact)
    return render_to_response("contacts/form.html", {
            "form": form,
            }, RequestContext(request))

这是urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<contact_id>\d+)/$', 'detail', name='contactdetailsurl'),
    url(r'^new/$',                 'new_contact', name='newcontacturl'),
    url(r'^(?P<contact_id>\d+)/edit/$','edit_contact', name='editcontacturl'),

]

错误指向我的site_base.html文件中的这一行:

       <li id="tab_first"><a href="
  {% url contacts.views.index %}
  "><i class="icon-book"></i> Contacts</a></li>

如果您需要更多信息,请与我们联系。谢谢!

1 个答案:

答案 0 :(得分:0)

错误告诉您应该使用视图的完整路径,例如'contacts.views.edit_contact'(假设应用程序名为contacts)。

但是,在Django 1.8中不推荐使用URL模式中的字符串,Django 1.10+不支持。你应该使用callables。您已使用可调用views.index作为index网址格式。

我会转换其余的网址格式,如下所示:

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<contact_id>\d+)/$', views.detail, name='contactdetailsurl'),
    url(r'^new/$', views.new_contact, name='newcontacturl'),
    url(r'^(?P<contact_id>\d+)/edit/$', views.edit_contact, name='editcontacturl'),
]