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