Reputation: 13
form.py:
from django import forms
class FormName(forms.Form):
name=forms.CharField()
email=forms.EmailField()
text=forms.CharField(widget=forms.Textarea)
views.py:
from django.shortcuts import render
from .forms import forms
def index(request):
return render(request,'basicapp/index.html')
def form_page(request):
Form = forms.FormName()
return render(request,'basicapp/form_page.html',{'form':Form})
I dont know what is wrong here! when I run server, it makes an error, saying ImportError : cannot import name 'forms' from 'basicapp'
.
Upvotes: 1
Views: 3234
Reputation: 19
You made two mistakes in your code
1) Your form file should be named forms.py and not form.pY
2) your views.py import code should be
from django.shortcuts import render
from basicapp import forms
Upvotes: 0
Reputation: 539
There is an issue in your view.py
replace below line
from .forms import forms
with
from <your app name>.forms import forms
You are getting this error because django is trying to find it in root folder where your manage.py exist.
Upvotes: -1
Reputation: 638
First of all, it looks like you have named your forms file, form.py and you are trying to access a module called forms. Rename form.py
file to forms.py
.
Second, you are trying to import forms
from your forms file. This is actually referencing forms
you imported via from django import forms
. You have a couple options here. In your view file you can either import .forms
or from .forms import FormName
I prefer the latter.
So, after you rename form.py
to forms.py
I would rewrite views.py
to look like this:
from django.shortcuts import render
from .forms import FormName
def index(request):
return render(request,'basicapp/index.html')
def form_page(request):
this_form = FormName()
return render(request,'basicapp/form_page.html',{'form':this_form})
Upvotes: 2