form.py Это вкладка моей формы.
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 Это вкладка моих моделей
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 Это моя вкладка URL в моем приложении "security_app"
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 Это вкладка просмотра.
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 Это моя базовая вкладка, от которой я наследую nav панель для других вкладок htmls
<!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>
регистрация. html Это моя учетная запись для регистрации пользователя
{% 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 %}
ОШИБКА Я сталкиваюсь с этой ошибкой при выполнении "python manage.py migrate" и "python manage.py runserver". Я не знаю, почему появляется эта ошибка.
(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