首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行python manage.py迁移和python manage.py runserver时收到错误

运行python manage.py迁移和python manage.py runserver时收到错误
EN

Stack Overflow用户
提问于 2020-04-28 19:41:00
回答 1查看 67关注 0票数 1

form.py,这是我的表单选项卡。

代码语言:javascript
复制
from django.contrib.auth.models import User
from security_app.models import UserProfileInfo
from django import forms
class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())
    class Meta():
        model = User
        fields = ('Username','email','password')
class UserProfileInfoForm(forms.ModelForm):
    class Meta():
        model = UserProfileInfo
        fields = ('portfolio_site','profile_pic')

models.py这是“我的模型”选项卡

代码语言:javascript
复制
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class UserProfileInfo(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    portfolio_site = models.URLField(blank=True)
    profile_pic = models.ImageField(upload_to='profile_pics',blank=True)
    def __str__(self):
        return self.user.username

urls.py这是我的应用程序"security_app“下的urls选项卡

代码语言:javascript
复制
from django.conf.urls import url
from security_app import views

app_name = 'security_app'

urlpatterns = [
  url(r'^register/$',views.register,name='register')
]

view.py这是视图选项卡。

代码语言:javascript
复制
from django.shortcuts import render
from security_app.forms import UserForm, UserProfileInfoForm
# Create your views here.
def index(request):
    return render(request,'security_app/index.html')
def register(request):
    registered = False
    if request.method == "POST":
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            if 'profile_pic' in request.FILES:
                profile.profile_pic = request.FILES['profile_pic']
            profile.save()
            registered = True
        else:
            print(user_form.errors,profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()
    return render(request,'security_app/registration.html',
                            {'user_form':user_form,
                             'profile_form':profile_form,
                             'registered':registered})

base.html这是我的基本选项卡,我从它继承导航条到其他htmls选项卡。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Base</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  </head>
  <body>
    <nav class="navbar navbar-default navbar-static-top">
      <div class="container">
        <ul class="nav navbar-nav">
          <li><a class="navbar-brand" href="{% url 'index' %}">S N A P P P R R</a></li>
          <li><a class="navbar-link" href="{% url 'admin:index' %}">Admin</a></li>
          <li><a class="navbar-link" href="{% url 'security_app:register' %}">Register</a></li>
          <!-- <li><a class="navbar-link" href="{% url 'index' %}">Images</a></li> -->
        </ul>
      </div>
    </nav>
    <div class="container">
    {% block body_block %}
    {# Anything outside of this will be inherited if you use extend.#}
    {% endblock %}
    </div>
  </body>
</html>

registration.html,这是我的注册选项卡,供用户注册

代码语言:javascript
复制
{% extends "security_app/base.html" %}
{% load staticfiles %}
{% block body_block%}

    <div class="jumbotron">
      {% if registered %}
      <h1>Thank you for registering</h1>
      {% else %}
      <h1>Register Here</h1>
      <h3>Fill out the form:</h3>

      <form enctype="multipart/form-data" method="post">
        {% csrf_token %}
        {{ user_form.as_p }}
        {{ profile_form.as_p }}
        <input type="submit" name="" value="Register">
      </form>

      {% endif %}
      </div>
    {% endblock %}

error 我在运行"python“和"python”时遇到了这个错误。我不知道为什么会出现这个错误。

代码语言:javascript
复制
(team) C:\Users\ragha\project\security>python manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\core\management\base.py", line 366, in execute
    self.check()
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = self._run_checks(
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\core\management\commands\migrate.py", line 64, in _run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\core\management\base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\urls\resolvers.py", line 407, in check
    for pattern in self.url_patterns:
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\urls\resolvers.py", line 588, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\urls\resolvers.py", line 581, in urlconf_module
    return import_module(self.urlconf_name)
  File "c:\users\ragha\anaconda3\envs\myenv\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\ragha\project\security\security\urls.py", line 19, in <module>
    from security_app import views
  File "C:\Users\ragha\project\security\security_app\views.py", line 2, in <module>
    from security_app.forms import UserForm, UserProfileInfoForm
  File "C:\Users\ragha\project\security\security_app\forms.py", line 4, in <module>
    class UserForm(forms.ModelForm):
  File "C:\Users\ragha\Envs\team\lib\site-packages\django\forms\models.py", line 267, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (Username) specified for User
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-28 20:36:22

在您的fields.py中,将第8行-字段=(“用户名”、“电子邮件”、“密码”)更改为字段=(‘用户名’、'email‘、'password')。字段名没有大写。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61488615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档