Form.py
из форм импорта django
из .models импортировать UserRegister
из django.contrib.auth.forms импортировать UserCreationForm
из django.core.Exceptions import ValidationError< /p>
form.py
from django.shortcuts import render
from django.http import HttpResponseRedirect ,Http404
from .forms import UserRegisterForm
from django.contrib import messages
from .models import UserRegister
products=[{
"slug": "Mustang-GT",
"About": """The Ford Mustang GT in its stunning white color is nothing short of a modern-day classic.
From the moment you lay eyes on it, the Mustang's sleek and muscular design commands attention and admiration.
The pristine white finish only adds to its allure, giving it a timeless, elegant look that stands out on the road.
Performance: Under the hood, the Mustang GT roars to life with its 5.0-liter V8 engine, delivering a thrilling 450 horsepower.
The acceleration is exhilarating, and the handling is precise, making every drive a memorable experience.
Whether you're cruising down the highway or taking on winding roads, the Mustang GT's performance is unmatched in its class.
Interior: Step inside, and you're greeted with a blend of classic muscle car ambiance and modern sophistication.
The leather seats are comfortable and supportive, perfect for long drives. The dashboard is well-designed, with intuitive controls
and a high-quality infotainment system that keeps you connected and entertained. Driving Experience: Driving the Mustang GT is an absolute joy.
The sound of the engine is music to any car enthusiast's ears, and the responsive steering makes you feel in complete control.
The car's suspension strikes a perfect balance between comfort and sportiness, ensuring a smooth ride even on less-than-perfect roads.
Conclusion: The Ford Mustang GT in white is a perfect blend of beauty, power, and sophistication. It's a car that not only performs
exceptionally well but also turns heads wherever it goes. Whether you're a long-time Mustang fan or new to the world of muscle cars,
this vehicle will exceed your expectations. Highly recommended for anyone looking for a thrilling and stylish driving experience.""",
"image": "images/car.jpg"
}]
def Home(request):
return render(request, "home.html")
def Login(request):
if request.method == "POST":
form = UserRegisterForm(request.POST)
if form.is_valid():
form_info = UserRegister(username=form.cleaned_data.get('username'), email=form.cleaned_data.get('email'), password=form.cleaned_data.get('password1'))
form_info.save()
messages.success(request, 'Your account has been created!')
return HttpResponseRedirect('/Products')
else:
print("Form is not valid")
print(form.errors)
else:
form = UserRegisterForm()
return render(request, 'login.html', {'form': form})
def Products(request):
return render(request, "products.html")
def about_product(request, slug):
for product in products:
if product["slug"] == str(slug):
return render(request, "about_product.html", {"product": product})
raise Http404("Product not found")
{% block title %}
Home
{% endblock %}
{% load static %}
{% block css_files %}
{% endblock %}
{% block content %}
{% csrf_token %}
Username
{{ form.username }}
{% if form.username.errors %}
{% for error in form.username.errors %}
{{ error }}
{% endfor %}
{% endif %}
Email
{{ form.email }}
{% if form.email.errors %}
{% for error in form.email.errors %}
{{ error }}
{% endfor %}
{% endif %}
Password
{{ form.password1 }}
{% if form.password1.errors %}
{% for error in form.password1.errors %}
{{ error }}
{% endfor %}
{% endif %}
Confirm Password
{{ form.password2 }}
{% if form.password2.errors %}
{% for error in form.password2.errors %}
{{ error }}
{% endfor %}
{% endif %}
Sign In
{% endblock %}
Ошибка: –
Последние несколько дней я столкнулся с проблемой, из-за которой я не могу загрузить данные формы в базу данных. Кроме того, теперь я получаю следующую ошибку:
Объект типа «UserRegister» не имеет атрибута «USERNAME_FIELD».
Form.py из форм импорта django из .models импортировать UserRegister из django.contrib.auth.forms импортировать UserCreationForm из django.core.Exceptions import ValidationError< /p> form.py [code]from django import forms from .models import UserRegister from django.contrib.auth.forms import UserCreationForm from django.core.exceptions import ValidationError
def __str__(self): return self.username [/code] views.py Создавайте здесь свои представления. [code]from django.shortcuts import render from django.http import HttpResponseRedirect ,Http404 from .forms import UserRegisterForm from django.contrib import messages from .models import UserRegister
products=[{ "slug": "Mustang-GT", "About": """The Ford Mustang GT in its stunning white color is nothing short of a modern-day classic. From the moment you lay eyes on it, the Mustang's sleek and muscular design commands attention and admiration. The pristine white finish only adds to its allure, giving it a timeless, elegant look that stands out on the road. Performance: Under the hood, the Mustang GT roars to life with its 5.0-liter V8 engine, delivering a thrilling 450 horsepower. The acceleration is exhilarating, and the handling is precise, making every drive a memorable experience. Whether you're cruising down the highway or taking on winding roads, the Mustang GT's performance is unmatched in its class. Interior: Step inside, and you're greeted with a blend of classic muscle car ambiance and modern sophistication. The leather seats are comfortable and supportive, perfect for long drives. The dashboard is well-designed, with intuitive controls and a high-quality infotainment system that keeps you connected and entertained. Driving Experience: Driving the Mustang GT is an absolute joy. The sound of the engine is music to any car enthusiast's ears, and the responsive steering makes you feel in complete control. The car's suspension strikes a perfect balance between comfort and sportiness, ensuring a smooth ride even on less-than-perfect roads. Conclusion: The Ford Mustang GT in white is a perfect blend of beauty, power, and sophistication. It's a car that not only performs exceptionally well but also turns heads wherever it goes. Whether you're a long-time Mustang fan or new to the world of muscle cars, this vehicle will exceed your expectations. Highly recommended for anyone looking for a thrilling and stylish driving experience.""", "image": "images/car.jpg" }]
def Login(request): if request.method == "POST": form = UserRegisterForm(request.POST) if form.is_valid(): form_info = UserRegister(username=form.cleaned_data.get('username'), email=form.cleaned_data.get('email'), password=form.cleaned_data.get('password1')) form_info.save() messages.success(request, 'Your account has been created!') return HttpResponseRedirect('/Products') else: print("Form is not valid") print(form.errors) else: form = UserRegisterForm() return render(request, 'login.html', {'form': form})
Последние несколько дней я столкнулся с проблемой, из-за которой я не могу загрузить данные формы в базу данных. Кроме того, теперь я получаю следующую ошибку:
Объект типа «UserRegister» не имеет атрибута «USERNAME_FIELD».