اذهب إلى المحتوى
  • 0

مشكلة return datetime.date.fromisoformat(value) TypeError: fromisoformat: argument must be str عند تهجير البيانات في دجانغو

Zeina Almakdisi

السؤال

 

models.py

from django.db import models
from django.contrib.auth.models import User 
from django.utils.translation import gettext_lazy as _
from django.db.models.signals import post_save
from django.utils.text import slugify


class Profile(models.Model):
  user        = models.OneToOneField(User , verbose_name=_("user"), on_delete=models.CASCADE)
  name          =models.CharField(_("Name:"),max_length=50)
  subtitle      =models.CharField(_("Who_II:"),max_length=50)
  address       =models.CharField(_("Address:"),max_length=50)
  address_detial=models.CharField(_("address_detial:"),max_length=50)
  number_phone  =models.CharField(_("number_phone:"),max_length=50)
  working_hours =models.CharField(_("working_hours:"),max_length=50)
  waiting_time  =models.IntegerField(_("waiting_time"), blank=True, null=True)
  who_I         =models.TextField(_("Who I"),max_length=250, blank=True, null=True)
  price         =models.IntegerField(_("Price is"), blank=True, null=True)
  facebook      =models.CharField(max_length=100,blank=True, null=True)
  twitter       =models.CharField(max_length=100,blank=True, null=True)
  google        =models.CharField(max_length=100,blank=True, null=True)
  
  

  image         = models.ImageField(_("Photo Profile"), upload_to='profile', blank=True, null=True)
  Specialist_doctor=models.CharField(_("Specialist:"),max_length=100, blank=True, null=True)
  slug          =models.SlugField(_("slug"), blank=True, null=True  )

  def save(self,*args,**kwargs):
    if not self.slug:
        self.slug=slugify(self.user.username)
        super(Profile,self).save(*args,**kwargs)
          

  def get_queryset(self):
    return
    models.Task.objects.all()
  

class Meta:
    verbose_name = _("Profile")
    verbose_name_plural= _("Profiles")

    def __str__(self):
        return '%s' %(self.user.username)

    def create_profile(sender , **kwargs):
            if kwargs['created']:
                Profile.objects.create(user=kwargs['instance'])

    post_save.connect(create_profile , sender=User)

urls.py:

from django.urls import path
from . import views
app_name='accounts'

urlpatterns = [
        path('', views.map, name='map'),
        path('doctors/', views.doctors_list, name='doctors_list'),
        path('login/', views.user_login, name='login'),
        path('signup/', views.signup, name='signup'),
        path('myprofile/', views.myprofile, name='myprofile'),
        path('update_profile/', views.update_profile, name='update_profile'),
        path('<slug:slug>/', views.doctors_detail, name='doctors_detail'),
]

views.py:

from django.shortcuts import render,redirect
from django.contrib.auth.models import User
from .models import Profile
from .forms import Login_Form,UserCreationForms,UpdateUserForm,UpdateProfileForm
from django.contrib.auth import authenticate , login 
from django.contrib.auth.decorators import login_required

def doctors_list(request):
    doctors = User.objects.all()

    return render(request, 'user/doctors_list.html',{
        'doctors' : doctors,
    })
    
def doctors_detail(request, slug):
    doctors_detail = Profile.objects.get(slug = slug)
    return render(request, 'user/doctors_detail.html',{
        'doctors_detail' : doctors_detail,
    })

def user_login(request):
    if request.method == 'POST':
        form = Login_Form()
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request , username=username , password=password)
        if user is not None:
            login(request , user)
            return redirect('accounts:doctors_list')
    else:
              form = Login_Form()
    return render(request, 'user/login.html',{
        'form':form
    })

def signup(request):
     if request.method == 'POST':
        form = UserCreationForms(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_date.get('username')
            password = form.cleaned_date.get('password')
            user = authenticate(username=username , password=password)
            login(request,user)
            return redirect('accounts:doctors_list')
     
     else:
        form = UserCreationForms()
     return render(request , 'user/signup.html'),{
        'form':form,
     }


@login_required()
def myprofile(request):
     return render(request, 'user/myprofile.html',{
    })

def update_profile(request):
    user_form = UpdateUserForm(instance=request.user)
    profile_form=UpdateProfileForm(instance=request.user.profile)
   
    if request.method == "POST":
         user_form = UpdateUserForm(request.POST , instance=request.user)
         profile_form=UpdateProfileForm(request.POST ,request.FILES,instance=request.user.profile)
         if user_form.is_valid and profile_form.is_valid:
           user_form.save()
           profile_form.save()
           return redirect('accounts:myprofile')
    return render(request, 'user/update_profile.html',{
         'user_form' : user_form,
         'profile_form':profile_form,
    })


def map(request):
      return render(request, 'user/map.html',{
    })

 

تم التعديل في بواسطة Zeina Makdisi
رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

المشكلة تمرير وسيط يختلف عن الوسيط الذي تنتظره الدالة والسبب انه دجانغو تقوم بأنشاء ملفات تلقائية مثلا رسالة الخطأ الثانية : تنتظر سلسلة نصية لانه يوجد شرط في احدى الملفات التلقائية التي تم توليدها (هل قمت بتعديل نمط قاعدة البيانات)؟ قد يتم إنشاء ملفات تهجير مختلفة جزئياً مما يمنع وجود التوافق مع قاعدة البيانات المختارة)

الحل: ان تقومي بحذفي جميع الملفات الموجودة ضمن مجلد migrations مع الاحتفاظ ب

__pycache__
__init__

وإعادة تهجير البيانات

python manage.py migrate

واعلامي بالنتيجة

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

لا يوجد خطأ في الكود الذي أرفقتيه , لذالك الخطأ على الأرجح قد يكون في ملف sqlite , احذف مجلد migrations وملف sqlite 

ثم بعد ذلك طبق التعليمات التالية

python manage.py makemigrations

ثم

python manage.py migrate 

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 44 دقائق مضت قال Haroun Taha:

أين يظهر الخطأ أو ارفقي رسالة الخطأ

Traceback (most recent call last):
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\utils.py", line 66, in __getitem__
    return self._engines[alias]
KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\Medical\Medical\manage.py", line 22, in <module>
    main()
  File "C:\Users\Lenovo\Desktop\Medical\Medical\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 375, in execute    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 361, in execute    
    self.check()
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 387, in check      
    all_issues = self._run_checks(
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 65, in 
_run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 377, in _run_checks    return checks.run_checks(**kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks  
    new_errors = check(app_configs=app_configs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\admin\checks.py", line 79, in check_dependencies
    for engine in engines.all():
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\utils.py", line 90, in <listcomp>        
    return [self[alias] for alias in self]
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\utils.py", line 81, in __getitem__       
    engine = engine_cls(params)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\backends\django.py", line 27, in __init__    self.engine = Engine(self.dirs, self.app_dirs, **options)
TypeError: Engine.__init__() got an unexpected keyword argument 'init_command'

C:\Users\Lenovo\Desktop\Medical\Medical>python manage.py migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.2/ref/databases/#mysql-sql-mode       
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, sessions
Running migrations:
  Applying accounts.0006_profile_join_us...Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\Medical\Medical\manage.py", line 22, in <module>
    main()
  File "C:\Users\Lenovo\Desktop\Medical\Medical\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 375, in execute    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 364, in execute    
    output = self.handle(*args, **options)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 83, in wrapped     
    res = handle_func(*args, **kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 232, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate  
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\migration.py", line 124, in apply   
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\operations\fields.py", line 110, in 
database_forwards
    schema_editor.add_field(
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\mysql\schema.py", line 44, in add_field
    super().add_field(model, field)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\schema.py", line 433, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\schema.py", line 161, in column_sql
    default_value = self.effective_default(field)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\schema.py", line 233, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 793, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1435, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1414, in get_prep_value
    value = super().get_prep_value(value)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1274, in get_prep_value
    return self.to_python(value)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1375, in to_python
    parsed = parse_datetime(value)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\dateparse.py", line 106, in parse_datetime  
    match = datetime_re.match(value)
TypeError: expected string or bytes-like object

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 11 دقائق مضت قال Zeina Makdisi:

Traceback (most recent call last):
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\utils.py", line 66, in __getitem__
    return self._engines[alias]
KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\Medical\Medical\manage.py", line 22, in <module>
    main()
  File "C:\Users\Lenovo\Desktop\Medical\Medical\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 375, in execute    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 361, in execute    
    self.check()
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 387, in check      
    all_issues = self._run_checks(
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 65, in 
_run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 377, in _run_checks    return checks.run_checks(**kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks  
    new_errors = check(app_configs=app_configs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\admin\checks.py", line 79, in check_dependencies
    for engine in engines.all():
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\utils.py", line 90, in <listcomp>        
    return [self[alias] for alias in self]
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\utils.py", line 81, in __getitem__       
    engine = engine_cls(params)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\backends\django.py", line 27, in __init__    self.engine = Engine(self.dirs, self.app_dirs, **options)
TypeError: Engine.__init__() got an unexpected keyword argument 'init_command'

C:\Users\Lenovo\Desktop\Medical\Medical>python manage.py migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.2/ref/databases/#mysql-sql-mode       
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, sessions
Running migrations:
  Applying accounts.0006_profile_join_us...Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\Medical\Medical\manage.py", line 22, in <module>
    main()
  File "C:\Users\Lenovo\Desktop\Medical\Medical\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 375, in execute    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 364, in execute    
    output = self.handle(*args, **options)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 83, in wrapped     
    res = handle_func(*args, **kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 232, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate  
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\migration.py", line 124, in apply   
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\operations\fields.py", line 110, in 
database_forwards
    schema_editor.add_field(
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\mysql\schema.py", line 44, in add_field
    super().add_field(model, field)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\schema.py", line 433, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\schema.py", line 161, in column_sql
    default_value = self.effective_default(field)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\schema.py", line 233, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 793, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1435, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1414, in get_prep_value
    value = super().get_prep_value(value)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1274, in get_prep_value
    return self.to_python(value)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1375, in to_python
    parsed = parse_datetime(value)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\dateparse.py", line 106, in parse_datetime  
    match = datetime_re.match(value)
TypeError: expected string or bytes-like object

 

يحدث "TypeError: expected string or bytes-like object" في Python عندما نمرر وسيطة من نوع مختلف إلى دالة تتوقع وسيطة (argument) string ، على سبيل المثال re.sub (). لحل الخطأ ، تأكد من تمرير وسيطة string إلى الدالة.

 def __str__(self):
     return '%s' %(self.user.username)# --> replace with  str(self.user.username)

وأنصحك باستعمال PostgreSQL مع django لأنه مدعوم بشكل كبير عكس MySql الذي استغنى عنه django منذ وقت 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...