Where to implement python classes in Django?

I learn Django own, and I cannot figure out where I am implementing the regular Python class. I mean, I don’t know where the Python classes that I write go to. How do they go to a separate file and then imported into views.py or are the classes implemented inside the views.py file?

Example I want to implement Class Alphabet , should I do this in a separate file inside the module or just implement the functions inside the views.py file?

+5
source share
4 answers

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:

 #myapp/views.py: from myapp.myclasses import Alphabet 

Or you can do it like this:

 django_proj1/ django_proj1/ myclasses.py myapp/ views.py 

And in your opinion:

 #myapp/views.py: from django_proj1.myclasses import Alphabet 

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 #<*** 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)) 

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://localhost:8000/myapp/ 

You should see:

You have requested the result: helloZ

If you comment on the following line in myapp/views.py :

  context = RequestContext(request, { #'x' : result #<*** Make a variable named x available in your template. }) 

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('', # 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'), ] 
+4
source

Django is just Python at the end of the day.

You can create new modules anywhere in your project and import them into your views, models, URLs, etc. It often happens that you would organize common utils (utils.py).

You can deliver data to your views in several ways, for example:

 from your_module import some_object class MyView(TemplateView): template_name = 'some_template.html' def get_context_data(self, **kwargs): context = super(MyView, self).get_context_data(**kwargs) context['my_data'] = some_object.some_method() return context 

And in some_template.html : {{ my_data }}

+3
source

It depends on the size of the Alphabet class. If this is a utility class, I would suggest adding a utils.py file, for example. But it’s perfectly normal to have classes in the views.py file, mainly those that handle the user interface. You decide.

+1
source

Unlike similar frameworks, you can put your Python code anywhere in your project if you can reference them later on the import path (although model classes are partially an exception):

  • Applications refer to their import path (or AppConfig import path). Although there is some magic involving test.py and models.py , most of the import / link time is pretty explicit.
  • Views refer to urls.py files, but are imported as a regular python import path.
  • Middlewares is referenced by strings that indicate the import path ending with their class name.
  • Other settings that you usually do not configure are also full import paths.

An exception to this evidence is:

  • models.py, test.py, admin.py: They have specific goals and may not exist, providing:

    • You will not need any model in your application and will provide AppConfig (and not just the application name) in INSTALLED_APPS .
    • You will not rely on autodiscover for admin classes in your application.
    • You do not want to do tests in your application or specify a non-standard path for running your application test command.
  • templates and static files: your project will rely on downloaders for your static files and your template files and, ultimately, look for brute force in each of your applications: their internal static/ and templates/ directories, if they exist, look for these files .

Everything else is just normal python code , and if you need to import them from any view, you just make a normal offer for them to import (since the view code is imported using the usual Python import mechanism).

+1
source

Source: https://habr.com/ru/post/1238429/


All Articles