Tôi không biết các lớp Python tôi viết ở đâu. Giống như họ truy cập vào một tệp riêng biệt và sau đó được nhập vào views.py. Ví dụ tôi muốn triển khai Bảng chữ cái lớp.
Nó chỉ là một vấn đề nhận được tuyên bố nhập khẩu của bạn chính xác:
django_proj1/
django_proj1/
myapp/
myclasses.py
views.py
Sau đó, theo quan điểm của bạn:
#myapp/views.py:
from myapp.myclasses import Alphabet
Hoặc, bạn có thể làm điều đó như thế này:
django_proj1/
django_proj1/
myclasses.py
myapp/
views.py
Và trong chế độ xem của bạn:
#myapp/views.py:
from django_proj1.myclasses import Alphabet
đáp ứng để bình luận:
Và sau khi tôi nhập thành công lớp học của tôi, làm thế nào để tôi vượt qua thuộc tính cho một mẫu HTML?
Dưới đây là trực tiếp từ số django tutorial chính thức.
myapp/views.py:
from django.shortcuts import render
from django.http import HttpResponse
from myapp.myclasses import Alphabet #<*** Import your class.
from django.template import RequestContext, loader #<*** Import stuff for the template.
# Create your views here.
def index(request):
alph = Alphabet()
result = alph.change('hello') #Your class produces some result here.
template = loader.get_template("myapp/index.html")
context = RequestContext(request, {
'x' : result #<*** Make a variable named x available in your template.
})
return HttpResponse(template.render(context))
Các directory structure
trông như thế này:
django_proj1/
django_proj1/
myapp/
myclasses.py
views.py
templates/ <***Create this directory
myapp/ <***Create this directory
index.html <***Create this file
myapp/templates/myapp/index.html:
{% if x %}
<div>The result you requested was: {{x}}</div>
{% else %}
<div>Sorry, couldn't get the result.</div>
{% endif %}
myapp/myclasses.py:
class Alphabet:
def change(self, word):
return word + 'Z'
Bắt đầu máy chủ:
.../my_django_projects/django_proj1$ python manage.py runserver
url trong trình duyệt của bạn:
http://localhost:8000/myapp/
Bạn sẽ thấy:
Kết quả mà bạn yêu cầu là: helloZ
Nếu bạn nhận xét ra dòng sau trong myapp/views.py
:
context = RequestContext(request, {
#'x' : result #<*** Make a variable named x available in your template.
})
Sau đó mẫu sẽ gửi phần else
của index.html
cho trình duyệt:
Rất tiếc, không thể nhận kết quả.
django_proj1/django_proj1/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from . import views
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'dj1.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
)
django_proj1/myapp/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Bất cứ nơi nào bạn muốn, nó chỉ là Python. – davidism
Đặt nó bất cứ nơi nào django sẽ tìm thấy nó. Tôi thường có một mô-đun utils cho các tiện ích đa năng. – fiacre
Cảm ơn bạn đã trả lời! Nhưng nếu tôi đặt nó vào một tệp riêng biệt và sau đó nhập nó, làm cách nào để in các thuộc tính trong một mẫu HTML? Tôi biết rằng thông qua các hàm trong views.py tôi phải trả về yêu cầu và tên mẫu. Tuy nhiên, tôi dường như không thể "chuyển" thuộc tính của tôi đến trang mẫu. – Silvestrini