SyntaxError:尝试迁移时语法无效

时间:2019-11-26 00:37:00

标签: python django django-models django-templates django-views

models.py

from django.db import models

class User(models.Model):
    first_name = models.CharField(max_length=128)
    last_name = models.CharField(max_length=128)
    email = models.EmailField(max_length=256, unique=True)

views.py

from django.shortcuts import render
form appTwo.moldels import User
# Create your views here.
def index(request):
    return render(request, 'appTwo/index.html')


def users(request):
    user_list = User.object.order_by('first_name')
    user_dict = {'users':user_list}
    return render(request, 'appTwo/users.html', context=user_dict)

result image

appTwo / urls.py

from django.conf.urls import path
from appTwo import views

urlpatterns = [
    path('', views.users, name='users'),
]

我尝试了迁移,但是会导致语法错误。堆栈跟踪如下:

File "/home/hamid/Desktop/my_django_stuff/project_two/proTwo/proTwo/urls.py", line 18, in <module>
    from appTwo import views
  File "/home/hamid/Desktop/my_django_stuff/project_two/proTwo/appTwo/views.py", line 2
    form appTwo.moldels import User
         ^
SyntaxError: invalid syntax
(myDjango) hamid@hamid-PC:~/Desktop/my_django_stuff/project_two/proTwo$

2 个答案:

答案 0 :(得分:2)

您拼写错误的模型:

form appTwo.moldels import User

form appTwo.models import User

答案 1 :(得分:2)

除了FBSO指出的“ moldels”中的错别字外,“ form”中的同一行中还存在另一个,应该是“ from”:

from appTwo.models import User