导入错误Django配置文件 [英] Import Error Django-profiles

查看:156
本文介绍了导入错误Django配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让django-profile工作。



我按照本手册(The Missing Manual)



so: p>


  • 我在同一个项目中工作我也用于django注册(所以没有应用程序创建!!!)

  • 在我的模板中,我创建了一个文件夹/ profiles(包括edit_profile.html)

  • 我的AUTH_PROFILE_MODULE设置为'myProjectName.UserProfile'

  • 我已经在我的项目文件夹models.py中创建了包含:

      from django.db import 

    class UserProfile(models.Model):
    user = models.ForeignKey(User,unique = True)
    first_name = models.CharField(_('first name') ,max_length = 100)
    middle_name = models.CharField(_('middle name'),blank = True,max_length = 100)
    last_name = models.CharField(_('last name'),max_length = 100)
    birth_date = models.DateField(_('birth date'),blank = True,null = True)


  • 在我的项目文件夹中,我创建了forms.py,与手册中的代码完全相同。

  • 在我的项目的urls.py中,我添加了: / p>

      from myProjectName.forms import ProfileForm 
    ('^ profiles / edit','profiles.views.edit_profile',{ 'form_class':ProfileForm,'success_url':'/ my / custom / url',}),
    (r'^ profiles /',include('profiles.urls')),

    所以现在我项目的整个urls.py文件是:



    <$来自django.conf.urls.defaults import的$ p code $ d从django.views.generic.simple import direct_to_template
    从django.contrib导入admin
    从myProjectName .forms import ProfileForm
    admin.autodiscover()

    urlpatterns = patterns('',
    (r'^ admin /',admin.site.urls),
    (r'^ accounts /',include('registration.urls') ),
    (r'^ $',direct_to_template,
    {'template':'index.html'},'index'),
    ('^ profiles / edit' .views.edit_profile',{'form_class':ProfileForm,'success_url':'/ my / custom / url',}),
    (r'^ profiles /',include('profiles.urls')) ,

    当我现在访问 http://127.0.0.1:8000/profiles/edit/ ,我收到这个错误:

     异常值:没有名为ourcrestmont.itaco.models的模块

    这是import.py位于forms.py ...



    我做错了什么?




编辑:



我的forms.py的代码:

从django.db导入模型
从django.forms导入ModelForm
from ourcrestmont.itaco.models import *

class ProfileForm(ModelForm):
class Meta:
model = Foo
exclude =('field1','field2','field3')

与手册中的代码完全一样,还是这段代码不好? (可以有人给另一个更好的代码?)

解决方案

所以导入语句似乎试图导入一个名为 ourcrestmont / itaco / models.py。确保存在。



(由django模型,看起来像是一个名为ourcrestmont的项目,名为itaco的应用程序)



如果存在,请确保itaco和ourcrestmont都有 init .py文件。



可以只有项目(这种情况下的导入行来自projectname.models import modelname),但我建议坚持使用标准布局,并将模型,表单和视图合并到项目下的应用程序中,即使你目前只看到你的项目有一个应用程序。 (因为没有,它至少有三个,因为注册和配置文件也是 django应用程序,他们只是你所做的以外的django应用程序,如果你看到我的意思,并且认为它可能会使依赖和包含错误更容易解决。当然,每个其他django应用程序将会假定大部分内容都在应用程序内)


I'm trying to get the django-profiles work.

I follow the steps of this manual ("The Missing Manual"),

so:

  • I work in the same project I also use for the django-registration (so no app created!!!)
  • In my templates I've created a folder "/profiles" (including edit_profile.html)
  • My AUTH_PROFILE_MODULE is set to 'myProjectName.UserProfile'
  • I've created in my project folder models.py containing:

    from django.db import models
    
    class UserProfile(models.Model):
        user = models.ForeignKey(User, unique=True)
        first_name = models.CharField(_('first name'), max_length=100)
        middle_name = models.CharField(_('middle name'), blank=True, max_length=100)
        last_name = models.CharField(_('last name'), max_length=100)
        birth_date = models.DateField(_('birth date'), blank=True, null=True)
    

  • In my project folder, I've created forms.py, with exactly the same code as in the manual
  • In urls.py of my project I've added:

    from myProjectName.forms import ProfileForm
        ('^profiles/edit', 'profiles.views.edit_profile',{'form_class':ProfileForm,'success_url':'/my/custom/url',}),
        (r'^profiles/', include('profiles.urls')),
    

    So now the whole urls.py file of my project is:

    from django.conf.urls.defaults import *
    from django.views.generic.simple import direct_to_template
    from django.contrib import admin
    from myProjectName.forms import ProfileForm
    admin.autodiscover()
    
    urlpatterns = patterns('',
        (r'^admin/', admin.site.urls),
        (r'^accounts/', include('registration.urls')),
        (r'^$', direct_to_template,
        { 'template': 'index.html' }, 'index'),
        ('^profiles/edit', 'profiles.views.edit_profile', {'form_class': ProfileForm,'success_url':'/my/custom/url',}),
        (r'^profiles/', include('profiles.urls')),
    )
    

    When I access now http://127.0.0.1:8000/profiles/edit/, I get this error:

    Exception Value:    No module named ourcrestmont.itaco.models
    

    That's the import located in forms.py ...

    What am I doing wrong?

EDIT:

The code of my forms.py:

from django.db import models
from django.forms import ModelForm
from ourcrestmont.itaco.models import *

class ProfileForm(ModelForm):
  class Meta:
      model = Foo
      exclude = ('field1','field2','field3',)

It's exactly the same code as in the manual, or is this code not good? (could anyone give an other, better code?)

解决方案

So that import statement appears to be attempting to import a file called "ourcrestmont/itaco/models.py". Make sure that exists.

(by django model, that looks like it is a project called ourcrestmont with an app called itaco)

If it exists, make sure that itaco and ourcrestmont both have init.py files in them.

You can just have the project (the import line in that case is from projectname.models import modelname) but I'd recommend sticking to the standard layout and putting your models, forms and views together in an App beneath a project, even if you only currently see your project as having one App. (Because it doesn't, it has at least three, since registration and profiles are also django apps, they're just django apps outside of what you've made, if you see what I mean, and thinking of it like that might make dependancy and inclusion bugs easier to solve. Certainly every other django app is going to assume most things are inside an app)

这篇关于导入错误Django配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆