Django tutorial for beginner to advanced - part 2 - Technic Hubs

Technic Hubs

Technic Hubs aims in providing hands-on experience on Machine Learning, Augmented Reality(AR), Virtual Reality(VR), Django(Web Development), Flutter and React(App Development), Internet of Things(IoT) with videos on Nepali.

Breaking

Friday, November 10, 2017

Django tutorial for beginner to advanced - part 2

In this tutorial we are going to start our first app. In django, app means any site or link. Suppose, in any website there is a blog as www.example.com/blog. Here, that blog is called app or any web link is app. That means website is just a combination of apps. Lets start making app.

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 tutorials
Django tutorial for behinner to advanced

No comments:

Post a Comment