0

As stated in the title, I keep getting an error when trying to create a form. It's on the line that has:

from collection.forms import contact_form

An I'm getting the error:

 File "/home/mike/CINS465/465proj/project/firstapp/views.py", line 2, in <module>
    from collection.forms import contact_form
ImportError: No module named 'collection'

Any idea where I'm going wrong? I'm new to django and this was pulled from a tutorial on creating a contact form. I thought collection was built-in to django

Edit:

from views.py

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from .models import *
from .forms import contact_form

# Create your views here.
def contact(request):
    form_class = contact_form

    return render(request, 'contact.html', {
        'form': form_class,
    })

from urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'contact/$',views.contact, name='contact'),
]

from contact.html

{% block title %}Contact - {{ block.super }}{% endblock %}

{% block content %}
<h1>Contact</h1>
<form role="form" action="" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
</form>
{% endblock %}

from forms.py

from django import forms

from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User

class contact_form(forms.Form):
    contact_name = forms.CharField(label='Contact Name', max_length=255)
    contact_email = forms.CharField(label='Contact Email',max_length=255)
    contact_message = forms.CharField(
        label='Contact Message',
        required=True,
        widget=forms.Textarea
    )

I didn't include code that was irrelevant (i.e. index page).

Mike1982
  • 439
  • 10
  • 26
  • 2
    isn't it called `collections`? – Nae Nov 02 '17 at 20:29
  • `collection` is an app being created in that tutorial, not a part of Django – helb Nov 02 '17 at 20:29
  • what is your directory structure look like? @Mike1982 – yash Nov 02 '17 at 20:31
  • hmm ok. I changed it to collections and get ImportError: cannot import name 'views'. – Mike1982 Nov 02 '17 at 20:35
  • What part of the directory structure? Like where my views, urls, and forms live? – Mike1982 Nov 02 '17 at 20:38
  • ah nvm, in my original code I had from .forms import *, which did what I needed it to do (in place of what they did in the tutorial). Thanks for responses – Mike1982 Nov 02 '17 at 20:44
  • @Mike1982 Is better that you understand this error, so it does not happen again. As it is, it tells you that can't find the view `contact_form`, so maybe you mispelled the name of the method. Check your views.py inside the collection app. – schrodingerscatcuriosity Nov 02 '17 at 20:50
  • thank you for your response. I checked everything made sure it's spelled correctly. However, my form is not displaying. Updating original post with full code. – Mike1982 Nov 02 '17 at 21:02
  • ah ha got it. I just realized I forgot to add {% block content %} {% endblock %} in my base. So stupid, lol – Mike1982 Nov 02 '17 at 21:27
  • Don't call you app `collections`. There's a module in the standard library with that name, and using the same module name is very likely to cause problems. https://docs.python.org/3/library/collections.html – Håken Lid Nov 02 '17 at 23:16
  • Have you manually renamed the folders or anything? I could see from "/home/mike/CINS465/465proj/project/firstapp/views.py" that view is within a module called `firstapp`. I will assume, forms.py is also in the same module, right? – Arun Karunagath Nov 02 '17 at 23:41
  • Also, try using absolute import, instead of relative import. – Arun Karunagath Nov 02 '17 at 23:42
  • yeah i saw that. I just have to use .forms import contact_form, which I assume is referring to the same directory that views.py is in. I think the tutorial had forms.py in a collection folder, which it didn't show. Thanks, I got it all figured out. – Mike1982 Nov 03 '17 at 01:53

1 Answers1

0

You need to create an empty file called __init__.py in the app directory to make it into a package. If you don't have that file, you cannot perform submodule imports.

touch /home/mike/CINS465/465proj/project/firstapp/collections/__init__.py
Håken Lid
  • 22,318
  • 9
  • 52
  • 67