I am trying to implement hospital api with to one-to-many relationship table (Subject - is Parent table, RawData - child table), with flask-api, marshmellow and flask-sqlalchemy frameworks. After implementing schemes, models and resource, I make post method with raw json, through postman in endpoint /api/upload/subject:
{
"first_name": "Johny",
"last_name": "Silverhand",
"raw_tomograms": [
{
"rawdata_id": 1,
"raw_name": "subject_1",
"path": "/srv/John_s_rawdata.zip",
"subject_id": 1
}
]
}
But get an error marshmallow.exceptions.ValidationError: {'raw_tomograms': {0: {'rawdata_id': ['Unknown field.'], 'raw_name': ['Unknown field.'], 'path': ['Unknown field.']}}}
Models:
#./api/models/subject.py:
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from api.database import db
class Subject(db.Model):
__tablename__ = "subject"
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(50), nullable=True)
last_name = db.Column(db.String(50), nullable=True)
raw_tomograms = db.relationship("RawData", backref="subject", lazy=True)
create_date = db.Column(db.DateTime(timezone=True), default=datetime.now())
update_date = db.Column(db.DateTime(timezone=True), onupdate=datetime.now())
def __repr__(self):
return "<Subject {self.id} {self.first_name>"
#./api/models/rawdata.py:
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from api.database import db
class RawData(db.Model):
__tablename__ = "raw_data"
id = db.Column(db.Integer, primary_key=True)
raw_name = db.Column(db.String(120), nullable=False)
path = db.Column(db.String(250))
create_date = db.Column(db.DateTime(timezone=True), default=datetime.now)
update_date = db.Column(db.DateTime(timezone=True), onupdate=datetime.now)
subject_id = db.Column(db.Integer, db.ForeignKey("subject.id"))
def __repr__(self):
return f"<Raw_data {self.id}>"
Schemas:
#./api/schemas/rawdata.py:
from marshmallow import Schema, fields, post_load, EXCLUDE
from api.models.rawdata import RawData
from api.models.subject import Subject
from api.database import db
from api.database import ma
class RawSchema(Schema):
rawdata_id = fields.Integer(),
raw_name = fields.String(),
path = fields.String(),
create_date = fields.DateTime(timezone=True)
update_date = fields.DateTime(timezone=True)
subject_id = fields.Integer()
#post_load
def make_rawdata(self, data, **kwargs):
return RawData(**data)
#./api/schemas/subjecta.py:
from marshmallow import Schema, fields, post_load
from epidetect_api.models.subject import Subject
from epidetect_api.database import db
#from epidetect_api.database import ma
from epidetect_api.schemas.rawdata_schema import RawSchema
class SubjectSchema(Schema):
"""
Subject Marshmallow Schema
Marshmallow schema used for loading/dumping Subject
"""
#raw_tomograms = ma.Nested(RawSchema, many=True)
class Meta:
model = Subject
#exclude =
id = fields.Integer(load=True, partial=True)
first_name = fields.String(allow_none=True)
last_name = fields.String(allow_none=True)
create_date = fields.DateTime(timezone=True)
update_date = fields.DateTime(timezone=True)
raw_tomograms = fields.Nested(RawSchema, many=True)
#post_load
def make_subject(self, data, **kwargs):
return Subject(**data)
Resource:
#./api/resources/subject_resource.py
import logging
from flask import request, jsonify, render_template
from flask_restful import Resource, abort
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound
import json
from api.database import db
from api.models.subject import Subject
from api.schemas.subject_schema import SubjectSchema
SUBJECTS_ENDPOINT = "/api/upload/subject"
logger = logging.getLogger(__name__)
class SubjectsResource(Resource):
def post(self):
"""
SubjectsResource POST method. Adds a new Subject to the database.
:return: Subject.id, 201 HTTP status code.
"""
subject = SubjectSchema().load(request.json)
print(request.json, flush=True)
try:
db.session.add(subject)
db.session.commit()
except IntegrityError as e:
logger.warning(
f"Integrity Error, this subject is already in the database. Error: {e}"
)
abort(500, message="Unexpected Error!")
else:
return subject.id, 201
Full error log:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2091, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2076, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/usr/local/lib/python3.7/site-packages/flask_restful/__init__.py", line 467, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/flask/views.py", line 84, in view
return current_app.ensure_sync(self.dispatch_request)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/flask_restful/__init__.py", line 582, in dispatch_request
resp = meth(*args, **kwargs)
File "/srv/api/resources/subject_resource.py", line 48, in post
subject = SubjectSchema().load(request.json)
File "/usr/local/lib/python3.7/site-packages/marshmallow/schema.py", line 720, in load
data, many=many, partial=partial, unknown=unknown, postprocess=True
File "/usr/local/lib/python3.7/site-packages/marshmallow/schema.py", line 904, in _do_load
raise exc
marshmallow.exceptions.ValidationError: {'raw_tomograms': {0: {'rawdata_id': ['Unknown field.'], 'raw_name': ['Unknown field.'], 'path': ['Unknown field.']}}}
I need to create POST method Registration Form i have this code (i am using django rest_framework):
Users.models
class User(AbstractBaseUser):
username = models.CharField(max_length=20, unique=True)
email = models.EmailField(unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELD = 'username'
class MyUserManager(CreateAPIView):
def post(request, *args, **kwargs):
username = request.data.get('username')
email = request.data.get('email')
password = request.data.get('password')
user = model(username=username, email=normalize_email(email))
user.set_password(password)
return user
users.serializers:
class MyUserManagerSerializers(serializers.ModelSerializer):
username = serializers.CharField(max_length=20)
email = serializers.EmailField()
password = serializers.CharField()
class Meta:
model = models.User
fields = ['email', 'username', 'password']
and models.views.py:
class UserManagerAPIView(ListCreateAPIView):
serializer_class = MyUserManagerSerializers
def UserRegistration(request):
user = UserManager.post(request)
serializers = MyUserManagerSerializers(user)
serializers.is_valid(raise_exception=True)
serializers.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
urls.py
urlpatterns = [
path('register', UserManagerAPIView.as_view()),
and installed_apps:
INSTALLED_APPS = ['users.apps.UsersConfig']
AUTH_USER_MODEL = 'users.User'
when i am send POST method to localhost:8000/register i always get error:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
The above exception (no such table: users_user) was the direct cause of the following exception:
File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/rest_framework/generics.py", line 242, in post
return self.create(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/rest_framework/mixins.py", line 19, in create
self.perform_create(serializer)
File "/usr/local/lib/python3.9/site-packages/rest_framework/mixins.py", line 24, in perform_create
serializer.save()
File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 205, in save
self.instance = self.create(validated_data)
File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 939, in create
instance = ModelClass._default_manager.create(**validated_data)
File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 453, in create
obj.save(force_insert=True, using=self.db)
File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 67, in save
super().save(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 763, in save_base
updated = self._save_table(
File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 906, in _do_insert
return manager._insert(
File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1416, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/debug_toolbar/panels/sql/tracking.py", line 198, in execute
return self._record(self.cursor.execute, sql, params)
File "/usr/local/lib/python3.9/site-packages/debug_toolbar/panels/sql/tracking.py", line 133, in _record
return method(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /register
Exception Value: no such table: users_user
I am new in django, but i checked docs, check stack overflow, checked othe forums and can't to find how to fix this error, may be it is my bad. Because i think my register form is so bad, but how can i fix this? I did all migrations, and always i see "succesfully!".
In your User model:
from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db import models
class UserManager(BaseUserManager):
# You have to override the usermanager!
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, username=email, **extra_fields) # Here I set the email as the username!
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email=None, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email=None, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
class CustomUser(AbstractUser):
email = models.EmailField('Email', unique=True)
USERNAME_FIELD = 'email' # Again!
REQUIRED_FIELD = ['first_name', 'last_name', ] # Here you can add the fields that you want or the fields that you maybe create!
def __str__(self):
return self.email
objects = UserManager()
In your admin.py:
from django.conf import settings
from django.contrib import admin, messages
from django.contrib.admin.options import IS_POPUP_VAR
from django.contrib.admin.utils import unquote
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import (
AdminPasswordChangeForm,
)
from .models import CustomUser # Here! You have to import your User not the default user!
from .forms import CustomUserCreationForm, CustomUserChangeForm # Import the forms tha you create and set bellow!
from django.core.exceptions import PermissionDenied
from django.db import router, transaction
from django.http import Http404, HttpResponseRedirect
from django.template.response import TemplateResponse
from django.urls import path, reverse
from django.utils.decorators import method_decorator
from django.utils.html import escape
from django.utils.translation import gettext, gettext_lazy as _
from django.views.decorators.csrf import csrf_protect
from django.views.decorators.debug import sensitive_post_parameters
csrf_protect_m = method_decorator(csrf_protect)
sensitive_post_parameters_m = method_decorator(sensitive_post_parameters())
#admin.register(User)
class UserAdmin(admin.ModelAdmin):
add_form_template = 'admin/auth/user/add_form.html'
change_user_password_template = None
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
(_('Permissions'), {
'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions'),
}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'password1', 'password2'),
}),
)
form = CustomUserChangeForm
add_form = CustomUserCreationForm
change_password_form = AdminPasswordChangeForm
list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
search_fields = ('username', 'first_name', 'last_name', 'email')
ordering = ('username',)
filter_horizontal = ('groups', 'user_permissions',)
def get_fieldsets(self, request, obj=None):
if not obj:
return self.add_fieldsets
return super().get_fieldsets(request, obj)
def get_form(self, request, obj=None, **kwargs):
"""
Use special form during user creation
"""
defaults = {}
if obj is None:
defaults['form'] = self.add_form
defaults.update(kwargs)
return super().get_form(request, obj, **defaults)
def get_urls(self):
return [
path(
'<id>/password/',
self.admin_site.admin_view(self.user_change_password),
name='auth_user_password_change',
),
] + super().get_urls()
def lookup_allowed(self, lookup, value):
# Don't allow lookups involving passwords.
return not lookup.startswith('password') and super().lookup_allowed(lookup, value)
#sensitive_post_parameters_m
#csrf_protect_m
def add_view(self, request, form_url='', extra_context=None):
with transaction.atomic(using=router.db_for_write(self.model)):
return self._add_view(request, form_url, extra_context)
def _add_view(self, request, form_url='', extra_context=None):
# It's an error for a user to have add permission but NOT change
# permission for users. If we allowed such users to add users, they
# could create superusers, which would mean they would essentially have
# the permission to change users. To avoid the problem entirely, we
# disallow users from adding users if they don't have change
# permission.
if not self.has_change_permission(request):
if self.has_add_permission(request) and settings.DEBUG:
# Raise Http404 in debug mode so that the user gets a helpful
# error message.
raise Http404(
'Your user does not have the "Change user" permission. In '
'order to add users, Django requires that your user '
'account have both the "Add user" and "Change user" '
'permissions set.')
raise PermissionDenied
if extra_context is None:
extra_context = {}
username_field = self.model._meta.get_field(self.model.USERNAME_FIELD)
defaults = {
'auto_populated_fields': (),
'username_help_text': username_field.help_text,
}
extra_context.update(defaults)
return super().add_view(request, form_url, extra_context)
#sensitive_post_parameters_m
def user_change_password(self, request, id, form_url=''):
user = self.get_object(request, unquote(id))
if not self.has_change_permission(request, user):
raise PermissionDenied
if user is None:
raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {
'name': self.model._meta.verbose_name,
'key': escape(id),
})
if request.method == 'POST':
form = self.change_password_form(user, request.POST)
if form.is_valid():
form.save()
change_message = self.construct_change_message(request, form, None)
self.log_change(request, user, change_message)
msg = gettext('Password changed successfully.')
messages.success(request, msg)
update_session_auth_hash(request, form.user)
return HttpResponseRedirect(
reverse(
'%s:%s_%s_change' % (
self.admin_site.name,
user._meta.app_label,
user._meta.model_name,
),
args=(user.pk,),
)
)
else:
form = self.change_password_form(user)
fieldsets = [(None, {'fields': list(form.base_fields)})]
adminForm = admin.helpers.AdminForm(form, fieldsets, {})
context = {
'title': _('Change password: %s') % escape(user.get_username()),
'adminForm': adminForm,
'form_url': form_url,
'form': form,
'is_popup': (IS_POPUP_VAR in request.POST or
IS_POPUP_VAR in request.GET),
'add': True,
'change': False,
'has_delete_permission': False,
'has_change_permission': True,
'has_absolute_url': False,
'opts': self.model._meta,
'original': user,
'save_as': False,
'show_save': True,
**self.admin_site.each_context(request),
}
request.current_app = self.admin_site.name
return TemplateResponse(
request,
self.change_user_password_template or
'admin/auth/user/change_password.html',
context,
)
def response_add(self, request, obj, post_url_continue=None):
"""
Determine the HttpResponse for the add_view stage. It mostly defers to
its superclass implementation but is customized because the User model
has a slightly different workflow.
"""
# We should allow further modification of the user just added i.e. the
# 'Save' button should behave like the 'Save and continue editing'
# button except in two scenarios:
# * The user has pressed the 'Save and add another' button
# * We are adding a user in a popup
if '_addanother' not in request.POST and IS_POPUP_VAR not in request.POST:
request.POST = request.POST.copy()
request.POST['_continue'] = 1
return super().response_add(request, obj, post_url_continue)
Create a forms.py and add:
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = ('email', )
labels = {'username': 'Username/Email'}
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.email = self.cleaned_data['username']
if commit:
user.save()
return user
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('email', )
That's it!
run command
python manage.py makemigrations users
I am trying to implement firebase authentication with email and password on my flask app. I am getting this error when I try to login or register.
Any help would be appreciated.
I am very new to coding so please ELI5 if possible. Any pointers on how to improve anything else on my code would be appreciated.
127.0.0.1 - - [18/Jul/2021 21:02:09] "GET /login HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask_socketio/__init__.py", line 46, in __call__
start_response)
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/engineio/middleware.py", line 74, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/realp/Desktop/firebaseflask-main/factory/users/views.py", line 63, in login
if session['user']:
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/werkzeug/local.py", line 377, in <lambda>
__getitem__ = lambda x, i: x._get_current_object()[i]
File "/Users/realp/Desktop/firebaseflask-main/env/lib/python3.7/site-packages/flask/sessions.py", line 84, in __getitem__
return super(SecureCookieSession, self).__getitem__(key)
KeyError: 'user'
from flask import render_template, Blueprint, redirect, request, abort, session, jsonify, make_response
from factory import db, auth, database
import random
from factory.models import Users, Cards, Subscription
from flask_login import login_user, current_user, logout_user, login_required
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import date, datetime
from cardvalidator import luhn
import stripe
import json
users = Blueprint('users', __name__)
u/users.route('/register')
def register():
if session['users']:
return redirect('/dashboard')
else:
rand = random.randint(0,100000000)
return render_template('register.html', random=rand)
u/users.route('/insert-user', methods=['POST'])
def reg_user():
if request.method == 'POST':
first_name = str(request.form['first_name'])
last_name = str(request.form['last_name'])
phone_number = str(request.form['phone_number'])
email = str(request.form['email'])
password = str(request.form['password'])
user = auth.create_user_with_email_and_password(email, password)
data = {
"first_name": first_name,
"last_name": last_name,
"phone_number": phone_number,
"free_trial": 1,
"email": email
}
push_user = database.child("users").push(data, user['idToken'])
user_data = auth.get_account_info(user['idToken'])
data_user = {
"email": user_data['users'][0]['email'],
"data": data
}
session['user'] = data_user
return 'success'
u/users.route('/login')
def login():
if session['user']:
return redirect('/dashboard')
else:
rand = random.randint(0,100000000)
return render_template('login.html', random=rand)
u/users.route('/login-user', methods=['POST'])
def login_user_function():
data_user = {}
if request.method == 'POST':
email = str(request.form['email'])
password = str(request.form['password'])
login = auth.sign_in_with_email_and_password(email, password)
# user = auth.refresh(login['refreshToken'])
user_data = database.child("users").get(login['idToken'])
for user in user_data.each():
if user.val()['email'] == email:
data_user = {
"email": email,
"data": user.val()
}
session['user'] = data_user
return 'success'
u/users.route('/logout')
def logout():
session['user'] = {}
return redirect('/login')
# Add new card
u/users.route('/add_card', methods=['POST'])
def new_card():
if request.method == 'POST':
user_id = int(request.form['user_id'])
card_number = str(request.form['card_number'])
expiry = str(request.form['expiry'])
cvc = int(request.form['cvc'])
card_type = str(request.form['card_type'])
card_zip = str(request.form['card_zip'])
check_card = Cards.query.filter_by(card_number=card_number).first()
if check_card is None:
# Insert card
insert_card = Cards(card_number=card_number, expiry=expiry, cvc=cvc, user_id=user_id, card_type=card_type, card_zip=card_zip)
db.session.add(insert_card)
db.session.commit()
return 'success'
else:
return 'existed'
u/users.route('/validate-card', methods=['POST'])
def card_valid():
if request.method == 'POST':
card_number = int(request.form['card_number'])
if luhn.is_valid(card_number):
return 'valid'
else:
return 'fake'
u/users.route('/new-subscription', methods=['POST'])
u/login_required
def new_subscription():
if request.method == 'POST':
card_id = int(request.form['card_id'])
selection = int(request.form['selection'])
email = current_user.email
# # Stripe key
api_key_test = 'sk_test_xxxxx'
public_key_test = 'pk_test_xxxxx'
stripe.api_key = api_key_test
token = stripe.Token.create(
card={
"number": get_card_data.card_number,
"exp_month": int(month),
"exp_year": int(year),
"cvc": get_card_data.cvc,
'address_zip': get_card_data.card_zip
},
)
# return token
customer = stripe.Customer.create(email=email, source=token)
customer_id = customer.id
# # PAYMENT INFORMATION
charge = stripe.Charge.create(
customer = customer_id,
amount = amount,
currency = 'usd',
description = 'Subscription'
)
# Add Subscription
new_subscription = Subscription(
card_id=card_id,
period=selection,
price=(amount/100),
last_payment=datetime.today().strftime('%Y-%m-%d-%H:%M:%S'),
user_id=current_user.id)
db.session.add(new_subscription)
db.session.commit()
return 'success'
If the user is not signed in, there is no key named user in session as you it will be defined when user sign in which raises error. You should check if there is any key user in session.
Rather using in login function.
if session['user']
you should use
if 'user' in session
I'm a beginner learning django i've checked multiple times but didn't find out where its returning tuple instead of dictionary can anyone help.
forms.py
from django import forms
from .models import ModelClas
class FormClass(forms.ModelForm):
passw = forms.CharField(widget=forms.PasswordInput())
repass = forms.CharField(widget=forms.PasswordInput())
def clean(self):
super(FormClass, self).clean()
inputpass = self.cleaned_data.get('passw')
inputrepass = self.cleaned_data.get('repass')
if inputpass != inputrepass:
raise forms.ValidationError("Password not matched. please type again.")
return inputpass, inputrepass
class Meta:
model = ModelClas
fields = '__all__'
models.py
from django.db import models
# Create your models here.
class ModelClas(models.Model):
fname = models.CharField(max_length=30)
lname = models.CharField(max_length=30)
email = models.EmailField()
mobile = models.IntegerField()
passw = models.CharField(max_length=30)
repass = models.CharField(max_length=30)
views.py
def signpage(request):
if request.method == 'GET':
form = forms.FormClass()
if request.method == 'POST':
form = forms.FormClass(request.POST)
if form.is_valid():
print("Validation Successfull")
print("FName :", form.cleaned_data['fname'])
print("LName :", form.cleaned_data['lname'])
print("Email :", form.cleaned_data['email'])
print("Mobile :", form.cleaned_data['mobile'])
print("Password :", form.cleaned_data['passw'])
print("Re-type Password :", form.cleaned_data['repass'])
form.save(commit=True)
return HttpResponseRedirect('/accounts/logout')
return render(request, 'registration/signup.html', {'form':form})
The error.
Even understanding this error is really daunting for me if anyone explain it would be great
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/signup/
Django Version: 3.0.6
Python Version: 3.6.8
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'UserRegis']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/ashish/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/ashish/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/ashish/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ashish/DjangoProjects/Django/userd/UserRegis/views.py", line 26, in signpage
if form.is_valid():
File "/home/ashish/.local/lib/python3.6/site-packages/django/forms/forms.py", line 180, in is_valid
return self.is_bound and not self.errors
File "/home/ashish/.local/lib/python3.6/site-packages/django/forms/forms.py", line 175, in errors
self.full_clean()
File "/home/ashish/.local/lib/python3.6/site-packages/django/forms/forms.py", line 378, in full_clean
self._post_clean()
File "/home/ashish/.local/lib/python3.6/site-packages/django/forms/models.py", line 385, in _post_clean
exclude = self._get_validation_exclusions()
File "/home/ashish/.local/lib/python3.6/site-packages/django/forms/models.py", line 345, in _get_validation_exclusions
field_value = self.cleaned_data.get(field)
Exception Type: AttributeError at /signup/
Exception Value: 'tuple' object has no attribute 'get'
The error is in the data returned by your method FormClass.clean(). The method should return a dict with the form fields as keys or not return anything at all (which is an implicit return None).
More info: https://docs.djangoproject.com/en/2.2/ref/forms/validation/#validating-fields-with-clean
My suggestion is to just remove the return statement from the method:
class FormClass(forms.ModelForm):
...
def clean(self):
super().clean()
inputpass = self.cleaned_data.get('passw')
inputrepass = self.cleaned_data.get('repass')
if inputpass != inputrepass:
raise forms.ValidationError("Password not matched. please type again.")
That should solve your error.
In this line you're returning a tuple consisting of your inputpass and inputrepass. Why?
return inputpass, inputrepass
You should return cleaned_data instead:
class FormClass(forms.ModelForm):
passw = forms.CharField(widget=forms.PasswordInput())
repass = forms.CharField(widget=forms.PasswordInput())
def clean(self):
super(FormClass, self).clean()
inputpass = self.cleaned_data.get('passw')
inputrepass = self.cleaned_data.get('repass')
if inputpass != inputrepass:
raise forms.ValidationError("Password not matched. please type again.")
return self.cleaned_data #**FIX HERE**
class Meta:
model = ModelClass
fields = '__all__'