I do not know where the Python classes that I write go to. How they go a separate file, and then imported into view.py. Example I want to implement Class Alphabet.
This is just a matter of the correctness of your import statement:
django_proj1/ django_proj1/ myapp/ myclasses.py views.py
Then, in your opinion:
Or you can do it like this:
django_proj1/ django_proj1/ myclasses.py myapp/ views.py
And in your opinion:
Reply to comment:
And after I have successfully imported my class, how do I go through the attributes for the HTML template?
Below is the official django tutorial .
Myapp / views.py
from django.shortcuts import render from django.http import HttpResponse from myapp.myclasses import Alphabet
directory structure as follows:
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'
Start the server :
.../my_django_projects/django_proj1$ python manage.py runserver
URL in your browser :
http:
You should see:
You have requested the result: helloZ
If you comment on the following line in myapp/views.py :
context = RequestContext(request, {
The template will then send the else part of index.html to the browser:
Sorry, I could not get the result.
django_proj1 / django_proj1 / urls.py
from django.conf.urls import patterns, include, url from django.contrib import admin from . import views urlpatterns = patterns('',
django_proj1 / MyApp / urls.py:
from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index, name='index'), ]