Django -- User.DoesNotExist 不存在?

发布于 2024-07-19 05:12:39 字数 511 浏览 10 评论 0原文

我正试图抓住姜戈。 我在 Eclipse 上使用 Pydev。 我写了一个简单的注册页面,但无法开始工作。 Eclipse 抱怨 User.DoesNotExist 未定义。 最有可能的是,我错过了一些微不足道的事情。 这是代码的相关部分:

from django.contrib.auth.models import User
...
class SignUpForm (forms.Form):
    ...
    def clean_username (self): 
        try:
            User.objects.get(username=self.cleaned_data['username'])
        except User.DoesNotExist:
            return self.cleaned_data['username']
        raise forms.ValidationError(USERNAME_ALREADY_IN_USE)
    ...

I'm trying to get hold of Django. I use Pydev on Eclipse. I have written a simple signup page that I can't get to work. Eclipse complains that User.DoesNotExist is undefined. Most likely, I am missing something trivial. Here's the relevant portion of the code:

from django.contrib.auth.models import User
...
class SignUpForm (forms.Form):
    ...
    def clean_username (self): 
        try:
            User.objects.get(username=self.cleaned_data['username'])
        except User.DoesNotExist:
            return self.cleaned_data['username']
        raise forms.ValidationError(USERNAME_ALREADY_IN_USE)
    ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

青瓷清茶倾城歌 2024-07-26 05:12:39

问题确实出在 PyDev 上,而不是你的代码上。 您所做的绝对正确,但 IDE 总是难以解析像 Python 这样的动态语言中的属性。 对于DoesNotExist异常,它是通过__metaclass__而不是通过正常的对象继承添加的,因此PyDev不太可能找到它。 然而,它绝对应该有效。

The problem is really with PyDev, not your code. What you have done is absolutely correct, but IDEs will always have difficulty resolving attributes in a dynamic language like Python. In the case of the DoesNotExist exception, it is added via a __metaclass__ rather than through normal object inheritance, so PyDev is unlikely to be able to find it. However, it should definitely work.

水水月牙 2024-07-26 05:12:39

我刚刚发现 Pydev 实际上有一个很好的解决方法。

转到窗口> 首选项,然后Pydev > 编辑> 代码分析

单击未定义选项卡,并将“DoesNotExist”添加到标题为将以下名称视为全局变量的文本框中。

I just discovered Pydev actually has a nice workaround for this.

Go to Window > Preferences, then Pydev > Editor > Code Analysis.

Click the Undefined tab and add "DoesNotExist" to the text box titled Consider the following names as globals.

神仙妹妹 2024-07-26 05:12:39

Pydev 对于这种情况有一个解决方法(当成员在运行时定义时)。
只需在导致警告的字符串末尾添加#@UndefinedVariable(或当光标位于“DoesNotExist”时在键盘上按ctrl+1),它就不会抱怨。

Pydev has a workaround for such cases (when the members are defined at runtime).
Just add #@UndefinedVariable at the end of the string which cause the warning (or ctrl+1 on keyboard when the cursor is at "DoesNotExist"), and it won't complain.

野鹿林 2024-07-26 05:12:39

Eclipse 可以解析通过 __metaclass__ 运行时创建的属性吗?

请注意,您从未在任何模型上定义 DoesNotExist ,并且在 django.db.models.base.Model 上也未定义它。

Can Eclipse resolve attributes created runtime via __metaclass__es?

Notice that you never define a DoesNotExist on any of your models and it is not defined on django.db.models.base.Model either.

刘备忘录 2024-07-26 05:12:39

您还可以通过不同的方式解决它:只需转到 User 类,并在文档字符串中添加 @DynamicAttrs。
这将告诉 PyDev 该类的属性是动态添加的,并且将使其不再抱怨像DoesNotExist 这样的“问题”。

You can also solve it in a different way: just go to the User class, and add @DynamicAttrs in the docstring.
This will tell PyDev that attributes of the class are added dynamically, and will make it not complain any longer for "issues" like DoesNotExist.

美羊羊 2024-07-26 05:12:39

Eclipse 抱怨 User.DoesNotExist 未定义。

你是什​​么意思? 你有 python 错误和堆栈跟踪吗? 此代码必须有效(如 文档)。 看起来像是日食问题。 只需运行开发服务器并查看它是否有效:

manage.py runserver

Eclipse complains that User.DoesNotExist is undefined.

What do you mean by that? Do you have python error and stack trace? This code have to work (as in documentation). Looks like an eclipse issue. Just run dev server and see if it works or not:

manage.py runserver
绾颜 2024-07-26 05:12:39

我在 VirtualEnv 中的 Ubuntu 上遇到了同样的问题,为了解决问题,我使用了这个片段。

http://djangosnippets.org/snippets/191/#c3091

此外,他进行了自定义带有代码的用户字段:

class UserField(forms.CharField):
    def clean(self, value):
        super(UserField, self).clean(value)
        try:
            User.objects.get(username=value)
            raise forms.ValidationError("Someone is already using this username. Please pick an other.")
        except User.DoesNotExist:
            return value

I have same problem on Ubuntu in a VirtualEnv to solve problem I have used this snippets.

http://djangosnippets.org/snippets/191/#c3091

In parituclar he make custom User Fields with code:

class UserField(forms.CharField):
    def clean(self, value):
        super(UserField, self).clean(value)
        try:
            User.objects.get(username=value)
            raise forms.ValidationError("Someone is already using this username. Please pick an other.")
        except User.DoesNotExist:
            return value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文