How to display my index.html template in django restframework?

0

I have a problem with a test that I have to submit to a selective process that I am performing. I've tried every possible and imaginable way but I could not. When trying to call an index.html file that is inside a templates / api folder so that it becomes the root of the url ( link ) django does not recognize it. Here are codes:

File urls.py from my sisquestoes folder (root, folder that has setings.py): PS: I already changed the name of this api and still can not locate the template.

urlpatterns = [
    path(r'admin/', admin.site.urls),
    path('', include('api.urls')),
    url(r'^', include('api.urls','app_name')),
]

The urls.py file in the api folder (which has the admin.py folder and the files and templates / api / index.html):

router = routers.DefaultRouter()
router.register('questoes', views.QuestoesView)
router.register('user', views.UserView)

app_name="api"
urlpatterns = [
path('', include(router.urls)),
url(r'^$', views.index, name='index'),
]

Function in views.py:

from django.shortcuts import render
from . import views

  def index(request):
      return render(request,'api/index.html')

I honestly spent the whole dawn trying to solve read almost all the documentation, I went through various paths and tutorials and nothing. I look forward to some kind and caring soul. Big hug.

    
asked by anonymous 08.05.2018 / 11:37

1 answer

0

The default for including urls in Django has changed in version 2.0 The url is no longer used, but rather the path .

With this change, the regex is no longer used to define a url (at least not with the path ).

Therefore:

# Raiz
urlpatterns = [
    path(r'admin/', admin.site.urls),
    path('', include('api.urls')),
    # url(r'^', include('api.urls','app_name')),
]


#views.py
router = routers.DefaultRouter()
router.register('questoes', views.QuestoesView)
router.register('user', views.UserView)

app_name="api"
urlpatterns = [
    path('', views.index, name='index'),
    path('', include(router.urls)),
    # url(r'^$', views.index, name='index'),
]

Remembering that the folder structure where index.html is located should be root / api / templates / api / index.html .

    
05.11.2018 / 21:31