Grace Nikole
Grace Nikole

Reputation: 103

ImportError at post/new al crear un formulario con Django

I'm new to DJango and I have some doubts about forms. Here is the code, I would appreciate your help :D

This is the error that appears to me: enter image description here

directories and files:

 blog
   |--_pycachce_
   |--migrations
   |--static
   |--templates
       |--blog
       |--base.html
       |--forms.py
       |--post_bio.html
       |--post_detail.html
       |--post_edit.html
       |--post_list.html
   |--models.py
   |--tests.py
   |--urls.py
   |--views.py
   |--_init_.py
   |--admin-py

templates/post_edit.html code:

{% extends 'blog/base.html' %}
  {% block content %}
    <h1>New post</h1>
    <form method="POST" class="post-form">{% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default">Save</button>
    </form>
{% endblock %}

urls.py code:

from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list),
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
url(r'^post/new/$', views.post_new, name='post_new'),
]

views.py code:

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

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})

def post_new(request):
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})

Upvotes: 0

Views: 39

Answers (1)

Alasdair
Alasdair

Reputation: 308849

It looks as if your forms.py is in your templates/blog directory. It should be in the main blog directory alongside models.py.

Upvotes: 2

Related Questions