2015-05-10 49 views
1

即時通訊新的Django和IM試圖寫一個非常簡單的應用程序沒有找到,但我被困,並有幾個問題:Django的頁面使用完整的URL

首先我的應用程序被稱爲「empleados」(西班牙語)我試圖創建一個簡單的索引視圖,它顯示了我的表中的所有條目,我可以通過localhost訪問網頁:8000/empleados,但我無法通過localhost訪問網頁:8000/empleados/index.html它不是應該是一樣的?

我當前文件夾樹是這樣的:

project_root/ 
    empleados/ 
    __init__.py 
    admin.py 
    forms.py 
    models.py 
    tests.py 
    urls.py 
    views.py 
    templates/ 
     empleados/ 
     index.html 
     add_checklist.html 

    project/ 
    __init__.py 
    settings.py 
    urls.py 
    views.py 
    wsgi.py 

我的項目/ urls.py是這樣的:

from django.conf.urls import include, url 
from django.contrib import admin 

urlpatterns = [ 
    # Examples: 
    # url(r'^$', 'femsa.views.home', name='home'), 
    # url(r'^blog/', include('blog.urls')), 
    url(r'^empleados/', include('empleados.urls')), 
    url(r'^admin/', include(admin.site.urls)), 
] 

而且empleados/urls.py看起來是這樣的:

from django.conf.urls import url 

from . import views 

urlpatterns = [ 
    url(r'^$', views.index, name='index'), 
    url(r'^$', views.add_checklist, name='add_checklist'), 
] 

最後我empleados/views.py看起來是這樣的:

from django.shortcuts import render, get_object_or_404 
from django.http import HttpResponse 
from .models import Empleado 
from empleados.forms import * 
def index(request): 
    empleado_list = Empleado.objects.all() 
    context = {'empleado_list': empleado_list} 
    return render(request, 'empleados/index.html', context) 
# Create your views here. 

def add_checklist(request): 
    # Get the context from the request. 
    context = RequestContext(request) 

    # A HTTP POST? 
    if request.method == 'POST': 
     form = CheckListForm(request.POST) 

     # Have we been provided with a valid form? 
     if form.is_valid(): 
      # Save the new category to the database. 
      form.save(commit=True) 

      # Now call the index() view. 
      # The user will be shown the homepage. 
      return index(request) 
     else: 
      # The supplied form contained errors - just print them to the terminal. 
      print (form.errors) 
    else: 
     # If the request was not a POST, display the form to enter details. 
     form = CheckListForm() 

    # Bad form (or form details), no form supplied... 
    # Render the form with error messages (if any). 
    return render_to_response('empleados/add_checklist.html', {'form': form}, context) 

所以我的問題是如何訪問index.html與完整的url和add_checklist.html,每次我嘗試訪問它們,我只是得到了404錯誤,在此先感謝您的幫助!

+1

路由的關鍵在於擁有可讀的URL,而無需提供訪問文件的確切名稱。我從來沒有這樣的要求,所以我不知道這個問題的最佳實踐是什麼,但我想你可以通過在你的「empleados.urls」像''r'^ index'中指定路線來實現這一點。 HTML $'''。有人請糾正我,如果我錯了。 – cezar

+0

這不是你如何做路由。首先,您不能擁有兩個具有完全相同模式的網址。 URL指向視圖,而不是模板。 –

+0

非常感謝您的意見,我知道這不再是一個慣例,但我只是在嘗試一些框架的東西! – user2465233

回答

1

與其他一些框架/服務器不同,django不會自動提供文件。您必須明確指定要提供的服務。

在你的情況下,添加到index.htmlurl做的伎倆:

url(r'^index.html$', views.index, name='index'), 
url(r'^new$', views.add_checklist, name='add_checklist'), 

我必須補充一點,使用index.html是一個廢棄的約定。 Django允許以更好的方式設計URL結構。看看this的例子。

+0

非常感謝您的意見,我知道這不再是一種慣例,但我只是想用框架來做一些事情! – user2465233