Django First Web App
To get started with making an app, open your command prompt in the same project folder and type:python manage.py startapp myapp
Here, myapp can be any name of your app i.e. website link name.
After executing this instruction you will see folders and files like:
myapp/
migrations/
__init__.py
admin.py
apps.py
views.py
models.py
tests.py
__init__.py
Lest discuss in detail:
To create a view in website, we will initially mess up with views.py. Open that file, and add:
def index(request):
my_dict = {'insert_content':"hello i am from first app"}
return render(request,'myapp/index.html',context=my_dict)
After this, you have to create a new file called urls.py in the same folder. Open that and type:
from django.conf.urls import url
from myapp import views
urlpatterns = [
url(r'^$', views.index, name = 'index')
]
Now let me make you clear what we have done, initially we created a view there to display in our website. Its under a function index from views.py. Then, we created urls.py file where we introduced url patterns to call that view from the url. We have to set that url in our main project also, so open our mysite folder, open urls.py inside it. And then:
change urls pattern like this:
add :
url(r'myapp/',include('myapp.urls'))
and dont use comma after that
Django tutorial for behinner to advanced |
No comments:
Post a Comment