Django 从 models.py 导入类

2024-02-01

使用这样的文件夹结构:

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

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

我试过了:

from . import models.class

from models import class

from projectname.models import class

from projectname import models.class

from project import class

但对于所有这些我都会遇到无效的语法错误

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

模型.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

你应该可以走了:)

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

update:

问题是(几乎!)当然你的模型以“7”开头 - 将其更改为字母字符,一切都会好起来,我(几乎!)确定:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django 从 models.py 导入类 的相关文章

随机推荐

  • 在 RMarkdown 中的标题页后插入空白页

    我试图在我的 RMarkdown 文档 pdf 输出 的标题页和目录之间插入空白页 我遵循了所描述的建议here https stackoverflow com questions 28962651 inserting title page
  • 数组的随机打乱

    我需要随机洗牌以下数组 int solutionArray 1 2 3 4 5 6 6 5 4 3 2 1 有什么功能可以做到这一点吗 使用集合来打乱原始类型数组有点过分了 自己实现该功能非常简单 例如使用费舍尔 耶茨洗牌 http en
  • 如何在 Apache httpclient 中使用指数退避策略?

    The docs https hc apache org httpcomponents client dev httpclient cache apidocs org apache http impl client cache Expone
  • 即使我使用 Content-Disposition: inline,PHP 也会强制下载 PDF 文件

    如果可能的话 我正在尝试在浏览器中显示 PDF 而且我知道我可以在 Chrome 中执行此操作 这就是我正在测试的版本 问题是 每次我尝试时 它都会提示下载 我正在使用 PHP 会话 所以我知道发送了一些无关的标头 所以我调用header
  • 使用 System.IO.Ports.SerialPort 类打开串行端口时出错

    我正在尝试使用 System IO Ports SerialPort 类打开并读取串行端口 我将串行端口从工具窗格 Visual Studio 2008 拖到我的 Windows 窗体应用程序上 我已经设置了属性网格 因此我可以在运行时轻松
  • Flutter:如何防止设备方向改变并强制纵向?

    我想阻止我的应用程序改变其方向并强制布局坚持 纵向 在 main dart 中 我放置了 void main SystemChrome setPreferredOrientations DeviceOrientation portraitU
  • 如何在 JVM 上确定任意格式(JPEG、PNG 等)图像文件的尺寸?

    我想浏览一个目录并挑选出所有图像 然后根据它们的尺寸执行一些操作 有哪些库可供我执行此操作 我正在 Clojure 中工作 但 JVM 上可用的任何东西都是公平的 提前致谢 with open r java io FileInputStre
  • 释放 malloc 失败

    我有这样的东西 char temp xyz temp char malloc sizeof somestring xyz char malloc sizeof temp xyz strrchar temp temp does not con
  • 将图像上传到 firebase 实时数据库或存储?

    我正在尝试将图像与其他字符串和双精度数据类型一起上传到 firebase 我现在想到两个选项 一个是将图像 Url 上传到 firebase 实时数据库 当我检索它时 我将检索图像 Url 并将其转换为图像 另一种方法是将图像文件上传到 F
  • 子进程的奇怪行为

    我有一个关于子进程行为的问题 这个测试程序的目的是运行Linux命令echo Hello wc使用管道 我的命令不起作用 结果调用了错误的 c 字符串 预期输出是 command 0 echo command 1 wc 但是 我得到 com
  • Google Apps 脚本 - 处理多个谷歌帐户

    我有一个自定义表单 可以向数据库发出一些请求来验证用户 我注意到 如果我有一个谷歌帐户 它可以正常工作 但如果有多个帐户 就不行了 我注意到的另一件事是脚本不会抛出任何错误 它只是不会从自定义表单返回结果 这就是我的自定义表单的样子 div
  • JUnit 测试期间静态初始化程序不运行

    我这里有一个有趣的 JUnit 问题 JUnit 4 12 我有一个只有静态方法的基类 由于它们的使用方式 它们必须是静态的 我从基类继承了其他类 所以 如果基类是Base 我们有ChildA and ChildB 大多数方法都包含在基类中
  • 如何获取运行任务的队列 - celery

    我是新使用芹菜 有一个问题 我有这个简单的任务 app task name test install queue def test install queue return subprocess call exit 0 shell True
  • 在 iOS13/iPadOS 13 中使用 dylib 调试偶发的应用程序崩溃

    更新到 iOS 13 x iPadOS 13 x 后 我们发现最近的 Testflight 版本偶尔会发生崩溃 我们还没有看到使用我们 13 之前发布版本的用户的报告 但似乎还没有多少人更新到 13 所以我们真的不知道 该应用程序是一个带有
  • 无法解析 \node_modules\electron 中的“fs”

    我正在使用电子反应样板并想使用电子dialog在 App tsx 中 const dialog require electron remote also tried with import const Hello gt const read
  • 如何判断哪个 Keras 模型更好?

    我不明白使用哪个输出精度来比较我的 2 个 Keras 模型 看看哪个模型更好 我是使用 acc 来自训练数据 之一还是 val acc 来自验证数据 之一 每个时期都有不同的 accs 和 val accs 我如何知道整个模型的 acc
  • R { ggplot2 } 是否可以查询绘图的刻度线是什么?

    使用来自的示例哈德利的网站 http had co nz ggplot2 scale continuous html gt m lt qplot rating votes data subset movies votes gt 1000 n
  • UITextView获取当前行

    有没有办法 欢迎疯狂黑客 将当前行作为 UITextView 的字符串获取 这包括自动换行等 例如 在本例中 该方法将返回 堆栈溢出 这不是很好吗 我 因为这是基于光标的当前行 它还可以根据光标的位置返回 这是我所做的测试 或 这么认为 我
  • gensim 错误:导入错误:没有名为“gensim”的模块

    我尝试导入 gensim import gensim 但出现以下错误 ImportError Traceback most recent call last
  • Django 从 models.py 导入类

    使用这样的文件夹结构 library django wsgi manage py static all my static files library init py models py settings py urls py views