Home > other >  DJANGO error "TypeError: expected string or bytes - like object"
DJANGO error "TypeError: expected string or bytes - like object"

Time:10-22

The code is as follows:
The from the django. Shortcuts import render, redirect
The import re
From the user. The models import user
The from the django. Urls import reverse
# the Create your views here.
#/user/register
Def register (request) :
"" "register "" "
. If the request method=='GET' :
Return render (request, 'register. HTML)
The else:
# to register processing
# 1. Receiving data
The username=request. POST. Get (" user_name ")
Password=request. POST. Get (' PWD)
Email=request. POST. Get (' email ')
Allow=request. POST. Get (' allow ')

# 2. Data validation
If not all (/username, password, email) :
# data incomplete
Return render (request, 'register. HTML' {' errmsg: 'data incomplete'})
# check email
If not re. Match (r "^ [a - z0-9] [\ w \ -] * @ [a - z0-9 \] + (\. [a-z] {2, 5})} {1, 2 $", email) :
Return render (request, 'register. HTML' {' errmsg: 'mailboxes illegal'})
If allow! :='on'
Return render (request, 'register. HTML' {' errmsg: 'please agree to agreement'})
# check whether user name repeated
Try:
User. Objects. The get (username=username)
Except the User. DoesNotExist:
# the user name does not exist
User=None
If the user:
# the user name already exists
Return render (request, 'register. HTML' {' errmsg: 'user name already exists'})
# 3. The business processing: user registration
User=user. Objects. The create_user (username, email and password)
User. Is_active=0
The user. The save ()
# 4. Return reply
Return redirect (reverse (' goods: index))





Perform error:
Internal Server Error:/user/register
Traceback (the most recent call last) :
The File "D: \ Python3 \ py3 \ lib \ site - packages, django, core, handlers \ exception py", line 47, in inner
The response=get_response (request)
The File "D: \ Python3 \ py3 \ lib \ site - packages, django, core, handlers \ base py", line 179, in _get_response
Callback_args response=wrapped_callback (request, * and * * callback_kwargs)
The File "E: \ Python advanced, fresh every day, the user \ views. Py", line 38, register in
User=user. Objects. The create_user (username, email and password)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ contrib \ auth \ models py", line 146, in the create_user
Return the self. _create_user (username, email, password, * * extra_fields)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ contrib \ auth \ models py", line 140, in _create_user
User. The save (using=self. _db)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ contrib \ auth \ base_user py", line 67, in the save
Super (). The save (* args, * * kwargs)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ base py", line 754, in the save
Force_update=force_update, update_fields=update_fields)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ base py", line 792, in save_base
Force_update, using update_fields,
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ base py", line 895, in _save_table
Results=self. _do_insert (CLS) _base_manager, using, fields, returning_fields, raw)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ base py", line 935, in _do_insert
Using=using raw=raw,
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ manager py", line 85, in manager_method
Return getattr (self get_queryset (), name) (* args, * * kwargs)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ query py", line 1249, in _insert
Return query. Get_compiler (using=using) execute_sql (returning_fields)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ SQL \ compiler py", line 1396, in execute_sql
For SQL, params in the self. As_sql () :
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ SQL \ compiler py", line 1341, in as_sql
For obj in self. Query. Objs
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ SQL \ compiler py", line 1341, in & lt; Listcomp>
For obj in self. Query. Objs
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ SQL \ compiler py", line 1340, in & lt; Listcomp>
[self. Prepare_value (field, self pre_save_val (field, obj)) for the field in fields]
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ SQL \ compiler py", line 1281, in prepare_value
Value=https://bbs.csdn.net/topics/field.get_db_prep_save (value, connection=self. Connection)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ fields \ set py", line 823, in get_db_prep_save
Return the self. Get_db_prep_value (value, connection=connection, prepared=False)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ fields \ set py", line 1378, in get_db_prep_value
Value=https://bbs.csdn.net/topics/self.get_prep_value (value)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ fields \ set py", line 1357, in get_prep_value
Value=(https://bbs.csdn.net/topics/super). Get_prep_value (value)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ fields \ set py", line 1217, in get_prep_value
Return the self. To_python (value)
The File "D: \ Python3 \ py3 \ lib \ site - packages \ django \ db \ models \ fields \ set py", line 1318, in to_python
Parsed=parse_datetime (value)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related