Reputation: 113
I'm new to Django and trying to understand the "correct" way to structure my project, in particular when and how to put separate functionality within an application.
The site I am building will include mainly static information (it's for a holiday cottage) which I'm providing with views from the main site project. I also want to include a calender, which will display availability in a sidebar, which I have started to build as an application "availabilityCalender" as it will be reused across most pages and I can see me using it in other projects.
What I can't figure out is how to hook this into a page view from my application. Most tutorials online approach applications as representing the entire page and link into a view from the urls.py of the project. Instead I'm want to add the application as a part of my page. The simplest way to represent what I'm trying to do is below:
from django.shortcuts import render
from django.http import HttpResponse
import availabilityCalender
def index(request):
availabilityCalender.views.monthView()
I was hoping that this would simply add the application's view into the index view. When testing this I get the error "errorAttributeError at /, 'module' object has no attribute 'views'".
Am I trying to use applications is a way the are not designed for or simply using the wrong approach? My apologies for what is almost certainly an incredibly simple question!
I got the above code to work by changing my import:
from availabilityCalender.views import monthView
def index(request):
return monthView(request)
Upvotes: 1
Views: 199
Reputation: 203
first of all your project should have a structure similar to this:
-/myProject
-/myproject
-settinngs.py
-views.py
-urls.py
-/templates
-mange.py
-/availabilityCalender
-models.py
-views.py
Now you need to make sure you add to your settings INSTALLED_APPS the app "myproject.availabilityCalender", Once this is done its quite simple to use and reuse your app in all project and further apps. In this specific case if you want to import and use your app on the myproject/views.py, just do:
from myproject.availabilityCalender.models import <--MODULES YOU NEED FOR YOUR VIEW-->
def monthView(request):
.
.
.
return render(request, 'index.html')
Upvotes: 0
Reputation: 55972
One way to do this would be to use template inheritance. Each one of your templates could extended a base template that includes your calendar.
Another way would be to put your calendar in its own template and to use the {% include %}
template tag.
If your calendar has data associated with it, the next issue would be getting that data to the templates. If you are including the calendar on every page of your site you could create a custom context processor that automatically adds the calendar data to every request.
If the calendar is only on some pages you could just load the data on a view by view basis. Perhaps you could extract the calendar functionality into a CalendarMixin view that loads the correct calendar data in get_context_data
.
Upvotes: 1