im practising using Django/Python to make a blog. 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.
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
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
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.