I've been having a look at Django and, from what I've seen, it's pretty darn fantastic. I'm a little confused, however, how I go about implementing a "home page" for my website? Would it be a separate app, or just a view within the project, or what?
There's no real rule for this, But one thing I like to do is actually arrange for the index access to redirect to another spot. If you prefer, though, you can just give the index page a plain view.
That said, It's probably a good idea to keep all your code in an actual app, so that you can refactor it more easily, and so that it appears on the python path as a normal module. Putting views in the project rather than an app seems to cause more headaches than it solves.
I just found my original approach (direct_to_template) is deprecated in Django 1.5
Instead, use a TemplateView to achieve the same result
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^$',
TemplateView.as_view(template_name='index.html'),
name='index'),
)
(For Django 1.4) You can setup a direct_to_template url within ./project/project/urls.py
from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^$', direct_to_template, { 'template': 'index.html'}),
# add urls to apps here
)
For both, place the template (index.html) in your TEMPLATE_DIRS root. This is one approach to create a homepage without implementing an entire app. There are many ways to make this happen as others have noted.
The easiest way is using Django's "Flatpages". See this link for more info: http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/
Related
I want to write a reusable Django application.
I tell my users to add the following to their urls.py
path('slack/', include(('slack_integration.urls', 'slack_integration'), namespace='slack_integration'),
And in my urls.py I want to have a view login_callback.
Now in my view, I need to get a value of slack_integration:login_callback.
I can trust the user that he/she will integrate it with slack_integration prefix and use it. But is this the best practise? Can I somehow get the name of the namespace for the app if user chooses a different name for it?
Thanks a lot!
Using namespace= within urls.py files is no longer supported, as it moves something specific to the Django app outside of the Python package that is the Django app.
The best practice now is to define the app_name within the urls.py file inside the Django app.
The old way: DON'T DO THIS (pre-Django 2.0)
the root urls.py
path('slack/', include(('slack_integration.urls', 'slack_integration'), namespace='slack_integration'),
The new way: DO THIS! (Django 2.0+)
the root urls.py
from django.urls import path, include
urlpatterns = [
path('slack/', include(('slack_integration.urls', 'slack_integration')),
]
slack_integration/urls.py
from django.urls import path
app_name = "slack_integrations"
urlpatterns = [
path('', HomeView.as_view(), name='home'),
]
As you can see, this keeps the namespace for the patterns within the app itself, along with the templates most likely to use it. The days of extra instructions on how to include an app are over! Good luck.
I am trying to create an index page for my home app using Django. I would like for the urls.py file to point directly to an index.html file in my templates folder within the home app. Therefore, I have the following:
urlpatterns = patterns('',url(r'^$',template_name='index.html'),)
This gives me an error when I load the page, however.
url() got an unexpected keyword argument 'template_name'.
All the resources I have looked at have a view instead of directly linking to an html, but I simply want to go to an index page. How can I achieve this? Thank you!
As per Django documentation:
This would be a proper way of doing it:
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^about/', TemplateView.as_view(template_name="about.html")),
)
I am extremely new to programming. I searched and found a few similar threads here, though unfortunately the answers in those threads didn't seem to fix my own issue.
Problem
I am working my way through the Django tutorials, and I am on the third tutorial:
https://docs.djangoproject.com/en/1.7/intro/tutorial03/
I'm not sure if this matters, but I am working on a Macintosh w/ Mavericks.
The root directory in which I am working is 'Test2'. The directory structure is:
/Users/me/src/collact/collact/test2/
Within 'test2', and per the previous two tutorials, I have '/polls' and '/templates' folders.
My polls/views.py file has the following text:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
My polls/urls.py file has the following text:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
Finally, in /test2/, I have a urls python file as well (.py). It has the text:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
As far as I can tell, this is what the tutorial prescribes. Unfortunately, when I run the server using: python manage.py runserver, the website gives me an error/404 message:
Page not found (404)
Request Method:
GET Request
URL: http://localhost:8000/polls/ Using the URLconf defined in
test2.urls, Django tried these URL patterns, in this order: ^admin/
The current URL, polls/, didn't match any of these. You're seeing this
error because you have DEBUG = True in your Django settings file.
Change that to False, and Django will display a standard 404 page.
Does anyone have any idea how I can fix this? I know that this is a really stupid question, but I have been trying to figure it out for the past day or two, and unfortunately I'm just not familiar enough with programming to solve this on my own.
I plan on having several different apps inside my django project, each for a new technology I want to play around with. As I work on each on, I want to have the root URL path redirect to the project I'm working on.
Directory Structure:
backyard/
my_project/
views.py
backyard/
urls.py
backyard/backyard/urls.py:
from django.conf.urls import patterns, include, url
from django.shortcuts import redirect
urlpatterns = patterns('',
url(r'^$', redirect('my_app/')),
url(r'^my_project/$', 'my_project.views.homepage'),
)
backyard/my_project/views.py
def homepage(request):
return render_to_response('my_project/index.html', {'data':data})
When I access the page at http:machine-name:8000/ I get an error saying The included urlconf backyard.urls doesn't have any patterns in it I most definitely have URLs in my urls.py, what is the issue?
A URLconf needs to map regexes to views -- the redirect function doesn't return a view; rather, it returns an HttpResponse. You can look at this question for an example of how to define a redirect directly in the URLconf.
How can I support old and new URI versions both working without breaking the reverse()?
For example, I have:
urlpatterns = patterns('',
url(r'^(old_part|new_part)/other/$', 'some_view'),
)
In this case /old_part/other/ and /new_part/other/ point to the same view but reverse() method fails because it doesn't know how to form link properly.
Also, what if we have url(r'^(old_part|new_part)/other/', include(sub_patterns)) how can it be handled?
Do you have any ideas?
Thanks for your help.
I suppose you are migrating. This means you don't want old url work, you want it to redirect to new url. Probably with 301 HTTP code (permanent redirect).
Having several urls for same content makes your site harder to use and hurts your SEO. Permanent redirect will tell Google and any other search engine to reindex page with new address.
You can do it this way in Django:
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^new_part/other/$', 'some_view'),
url(r'^old_part/other/$',
RedirectView.as_view(url='new_part/other/', permanent=True)),
)
If you need to capture everything with a subpath, you can capture url ending and add it to redirect url this way:
urlpatterns = patterns('',
url(r'^new_part/other/$', include(sub_patterns)),
url(r'^old_part/other/(?P<rest>.*)$',
RedirectView.as_view(url='new_part/other/%(rest)s', permanent=True)),
)
You can use redirect_to generic view in Django 1.4 and earlier.
If you want without a redirect, you could try this
url(r'^(?P<url>old_part|new_part)/other/$', 'some_view', name='some_view'),
Then your view will look like this
def some_view(request, url):
...
Then call reverse like this:
# will return /old_part/other/
reverse('some_view', kwargs={'url': 'old_part'})
# will return /new_part/other/
reverse('some_view', kwargs={'url': 'new_part'})
Just redirect the old urls to the new ones (with a 301 Moved Permanently).
NB : if you really insist on supporting both sets of urls (not a good idea IMHO), you'll have to have two distinct url patterns, and choose which one reverse() should resolve to.