how to move the template? - python

please tell me how to move the view (view.py) in the directory "views".
Now the structure of my folders and files is as follows:
proj1(catalog)
views.py(file)
manage.py(file)
proj1(catalog)
wsgi.py(file)
urls.py(file)
settings.py(file)
__init__.py(file)
views(catalog)
urls.py following content:
from django.conf.urls import patterns, include, url
import views
urlpatterns = patterns('',
url('^$', views.hello),
url('^datetime$', views.current_datetime),
url('^dt$', views.current_datetime),
url('^dt/(\d{0,2})$', views.current_datetime2),
)
I need to file located in the directory view.py proj1/proj1 /.
wherein static pages is still made ​​available to the browser.
the problem is that I do not know how to change the code in the files: urls.py, settings.py

There is no need to change views, just do in settings.py
TEMPLATE_DIRS = (
'/path/to/proj1/proj1/views/', # or another path with templates
'django/contrib/admin/templates/'
)

Related

Django URLs file redirecting the different URLs to the wrong html files

My Django site isn't working properly. It's hard to explain but when I run my Django web server and go to http://127.0.0.1:8000/hello/ I see "Hello, World!" as expected. But when I go to http://127.0.0.1:8000/dashboard/ I see the same thing, when I should be seeing "Hello". There is to much code to put on stack overflow and it won't make sense so I made a GitHub repo https://github.com/Unidentified539/stackoverflow.
So to simplify your life
urlpatterns = [
path("",views.pomo,name="pomo"),
path("agenda/",views.agenda,name="agenda"),
path("notes/",views.notes,name="notes"),
]
this is your urls.py file in you app
just type this in your project urls.py so you don’t make a mess with paths and includes
urlpatterns = [
path('admin/', admin.site.urls),
path("",include('pomofocus.urls'))
]
this is just an example you need to enter your own paths and views
What's your code problem?
According to your code, you have two same paths for the home page, and when you enter http://127.0.0.1:8000/hello/ Django look at the project-level urls.py (in your code djangoProject1/urls.py) and knows that must go in app-level urls.py (in your code practice/urls.py) and in this file, you have two same paths, and Django uses the first one so you get write page because first one is related to hello view and when you type http://127.0.0.1:8000/dashboard/ Django again look at the project-level urls.py and knows that must go in app-level urls.py and in this file again use the first path and you get incorrect HTML file (because both paths are the same and Django always use the first one)
How to fix this?
in project-level url.py set both paths to '' and in app-level set your project-level paths. (in other words, replace paths in app-level and project-level
djangoProject1/urls.py:
import practice.urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('practice.urls')),
]
practice/urls.py:
from django.urls import path
from practice import views
urlpatterns = [
path('hello/', views.hello_world, name='hello_world'),
path('dashboard/', views.dashboard, name='dashboard')
]
Your code should be like in
practice/urls.py
from django.urls import path
from practice import views
urlpatterns = [
path(" ", views.hello_world, name='hello_world'),
path("dashboard", views.dashboard, name='dashboard')
]
Project1 urls.py
import practice.urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(" ", include('practice.urls')),
]

Django de-structuring of URLs

I am learning the Django Web Framework but just could not work my head around this logic.
Let's say we have a Django App that has a very big urls.py where all the patterns are stored here. How would one go about splitting this into a urls folder, wherein it contains multiple 'urls.py' files? And not only that, to have a nested directory within the urls folder as well.
Example project structure of what I am thinking of:
>main_base
>__init__.py
>asgi.py
>settings.py
>wsgi.py
>urls.py
>secondary_app
>admin.py
>apps.py
>models.py
>__init__.py
>views
>home.py
>idea.py
>project.py
>business_partners
>customers.py
>vendors.py
>urls
>__init__.py
>home.py
>idea.py
>project.py
>business_partners
>__init__.py
>customers.py
>vendors.py
in main_base/urls,
I would have the following:
from django.urls import path, include
urlpatterns = [
path('', include('secondary_app.urls')),
]
in secondary_app/urls/init.py,
I would expect to do the following:
from django.urls import path, include
urlpatterns = [
path('', include('secondary_app.urls.home')),
path('', include('secondary_app.urls.idea')),
path('', include('secondary_app.urls.project')),
path('', include('secondary_app.urls.business_partners')),
]
And lastly in secondary_app/urls/business_partners/init.py,
I would do the following:
from django.urls import path, include
urlpatterns = [
path('', include('secondary_app.urls.business_partners.customers')),
path('', include('secondary_app.urls.business_partners.vendors')),
]
I have tested it and it works all the way up till the secondary_app/urls directory level.
But once I go one more level down, it throws a reverse match not found error.
Example error:
NoReverseMatch at /
Reverse for 'bp-customers' with arguments '('',)' not found. 1 pattern(s) tried: ['bp/customers']
Any help is really really appreciated!
If you want your views in separate files do this (assuming CBV):
main_base/urls.py:
from django.urls import path, include
urlpatterns = [
path('', include('secondary_app.urls')),
]
secondary_app/urls.py:
from django.urls import path
from .views.home import HomeViewName
from .views.idea import IdeaViewName
urlpatterns = [
path('', HomeViewName.as_view(), name='home'),
path('idea/', IdeaViewName.as_view(), name='idea'),
### other views
]
This way your views will be in separate files and you will have routing to them
You should read official docs on django routing because it will answer your questions in greater detail.
When you create a django project with django-admin startproject myprojectand a app with django-admin startapp myapp the default django project layout is like:
-basedir/
--myproject
---asgi.py
---__init__.py
---__pycache_/
---settings.py
---urls.py
---wsgi.py
--myapp
---migrations/
---admin.py
---apps.py
---__init__.py
---models.py
---tests.py
---views.py
---manage.py
Base project directory and also every django app comes with urls.py file.There is a couple benefits for this structure but mainly keeps all related urls in it's own appdir to keep maintenance easier. Than you can include your apps urls in myproject/urls.py. For example :
in myproject/urls.py
from django.urls import path, include
urlpatterns = [
path('', include('myapp.urls')),
]
But if you would like to change the layout of a django project you can try
basedir/
--root_configuration/
--root_apps/
to keep your project files in one file and all the django apps in one file

Why is my urls.py file not being detected?

I just created a new app for my django project and have created a urls.py, models.py, views.py, and serializers.py. However, my project is not detecting the URLs in the urls.py file. If I go to a different app in my project and edit the urls.py file, the system detects it. However, it will not detect it in the new one. Here is the urls.py file:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^knorket-kaitongo-integration/$', views.KnorketKaitongoIntegration.as_view(),
name='knorket_kaitongo_integration'),
]
Does anyone know why this wouldn't be detected? It's not showing up on the API screen either.
Django does not autodetect any files
You need to install them using the installed_apps list and register URLs.
In the Root application, you should find a urls.py file, in that file you need to register your URLs.
You need to import application URLs in Root urls.py and add them in something like this.
urlpatterns = [
path('^', include(urls))
]

Error: Template Does Not Exist

I have two apps in one project the main apps call pages contain Home and About page the second apps is contact page.
Here is my contact/urls.py in contact apps:
from .views import ContactFormView
urlpatterns = patterns('',
# URL pattern for the ContactView
url(regex=r'^contact/$', view=ContactFormView.as_view(), name='contact'),
)
the main apps pages/urls.py :
from django.conf.urls import patterns, url
from .views import AboutPageView, HomePageView
urlpatterns = patterns('',
url(regex=r'^$', view=HomePageView.as_view(), name='home'),
url(regex=r'^about/$', view=AboutPageView.as_view(), name='about'),
)
in main apps pages/views.py :
from vanilla import TemplateView
class HomePageView(TemplateView):
template_name = "pages/home.jade"
class AboutPageView(TemplateView):
template_name = "pages/about.jade"
class ContactFormView(TemplateView):
template_name = "contact/email_form.jade"
I get this error say django.template.base.TemplateDoesNotExist
TemplateDoesNotExist: /contact/email_form.jade
I am not sure why, the setting file is OK I can open the main page with the contact page in the menu nav when I click contact I get the error above. Any helps appreciate
Thanks
You should have the email_form.jade file inside /templates/contact. Or place it in /templates/pages and change the line to template_name = "pages/email_form.jade".
Check to make sure you don't have the any of the parent directories the template is in marked as static in app.yaml.
App Engine stores and serves static files separately from application files. Static files are not available in the application's file system.
If you do either find a separate directory to place static files or you can optionally use
application_readable: True in the configuration block for static files which allows the filesystem to read them.
See here for more details

Django : error in importing the url file of an app

I am newbie in django framework
My project urls.py has the following code
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^rango/$',include(rango.urls)), #my test appliaction
url(r'^admin/', include(admin.site.urls)),
)
I have created an app called rango , that i have imported in the main project url file.
Now it throwing the following error, when I am trying to access url .../rango/
Exception Value: name 'rango' is not defined
I can see that in that python path is not correctly set up.
This is the directory structure of my project
project/
project/__init__.py
project/urls.py
rango/__init__.py
valuable advice required.
url(r'^rango/$',include(rango.urls)),
should read:
url(r'^rango/',include('rango.urls')),
No $ and use quotes 'rango.urls'. Because rango without the quotes is not defined.

Categories