简体   繁体   English

初学者 Django:Python ImportError 和 ModuleNotFoundError

[英]Beginner Django: Python ImportError and ModuleNotFoundError

im practising using Django/Python to make a blog.我正在练习使用 Django/Python 制作博客。 It has been going well but got stuck recently with Two errors, the first has gone now but comes back when I move things around.它一直进展顺利,但最近因两个错误而陷入困境,第一个现在已经消失,但当我移动东西时又回来了。 the second is the current error with the current state.第二个是当前 state 的当前错误。

djangogirls/mysite/urls.py", line 3, in <module>
from . import views
ImportError: cannot import name 'views' from 'mysite'

current error:当前错误:

djangogirls/blog/views.py", line 4, in <module>
from forms import PostForm
ModuleNotFoundError: No module named 'forms'

Here are some of my files:以下是我的一些文件:

views.py视图.py

from django.shortcuts import render
from django.utils import timezone
from .models import Post
from django.shortcuts import render, get_object_or_404


# Create your views here.



def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

urls.py网址.py

from django.urls import path 
from . import views

urlpatterns = [
    path('', views.post_list, name='post_list'),
    path('post/<int:pk>/', views.post_detail, name='post_detail'),
    path('post/new/', views.post_new, name='post_new'),
]

forms.py forms.py

from django import forms
from .models import Post

class PostForm(forms.ModelForm):

    class Meta:
        model = Post
        fields = ('title', 'text',)

Not completely sure about the structures yet (very beginner), so any advice would be so appreciated还不完全确定结构(非常初学者),所以任何建议都会非常感激

add.添加。 before forms... just like you are importing from.models the Post model, import from.forms.在 forms...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM