Django导入类来自models.py [英] Django Import Class From models.py

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

问题描述

使用这样的文件夹结构:

  library / 
-django.wsgi
-manage .py
-static /
- 我的静态文件
-library /
--__init__.py
--models.py
- settings.py
--urls.py
--views.py
--wsgi.py
--templates /
---我打算存储的地方我所有的模板

如何在我的views.py中导入一个在models.py中定义的类?



我尝试过:

  import models.class 

from models import class

from projectname.models import class

from projectname import models.class

从项目导入类

但是对于所有这些我得到无效的语法错误



views.py

  from django.core.context_processors import csrf 
from django导入重定向,从django.contrib.auth导入验证
,从django.contrib.auth.decorators导入login_required
从django.contrib.auth.models导入用户
from django.http import HttpResponse
from django.contrib import message
from django.template import RequestContext,loader
from django.contrib.auth import logout

从library.models import 7DTagmap

models.py



$ _ code从__future__导入unicode_literals

从django.db导入模型

class 7DTagmap(mo dels.Model):
id = models.IntegerField(primary_key = True)
tag_id = models.CharField(max_length = 50L)
st_tag_id = models.IntegerField()
class Meta :
db_table ='7d_tagmap'

错误:

 无效语法(views.py,第11行)
异常类型:SyntaxError
异常值:无效语法(views.py,第11行)


解决方案

使用:

  from library.models import MyClass 

你应该很好:)



(基本结构是< app> .models import< ModelName> c $ c>)



更新:



问题是(几乎! )肯定你的模型以'7'开头 - 把它改成一个字母,一切都会很好,我(几乎!)肯定:)


Using a folder structure like this:

library/
-django.wsgi
-manage.py
-static/
    --all my static files
-library/
    --__init__.py
    --models.py
    --settings.py
    --urls.py
    --views.py
    --wsgi.py
    --templates/
        ---where i plan to store all my templates

How can i import a class in my views.py that is defined in models.py?

I've tried:

from . import models.class

from models import class

from projectname.models import class

from projectname import models.class

from project import class

But for all those i get invalid syntax errors

views.py

from django.core.context_processors import csrf
from django.shortcuts import redirect, render
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.contrib import messages
from django.template import RequestContext, loader
from django.contrib.auth import logout

from library.models import 7DTagmap

models.py

from __future__ import unicode_literals

from django.db import models

class 7DTagmap(models.Model):
   id = models.IntegerField(primary_key=True)
    tag_id = models.CharField(max_length=50L)
    st_tag_id = models.IntegerField()
    class Meta:
        db_table = '7d_tagmap'

error:

invalid syntax (views.py, line 11)
Exception Type: SyntaxError
Exception Value:    invalid syntax (views.py, line 11)

解决方案

use:

from library.models import MyClass

and you should be good to go :)

(the basic structure is from <app>.models import <ModelName>)

update:

the problem is (almost!) certainly that your model begins with '7' -- change it to a letter character, and all will be well, I'm (almost!) sure :)

这篇关于Django导入类来自models.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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