301 redirect using Django urls.py - django

Hi I'm looking to setup some 301 redirects for some urls that have been changed on a site for a client.
I was thinking the following might work:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic import TemplateView, RedirectView
from cms.sitemaps import CMSSitemap
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^contact/', include('contact.urls')),
url(r'^case-study/', include('casestudies.urls')),
url(r'^contact-us/', include('contact.urls')),
url(r'^news/', include('news.urls')),
url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^', include('cms.urls')),
url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
url(r'^old-link/$', RedirectView.as_view(url='new-link/'), name='new-link'),
)
This seems to make sense to me but this doesn't seem to work. Now i am extremely new to Django so apologies if this is a really simple query.

Related

django url is giving page not found (404) error

I am getting page not found(404) error for votings app that I created from datacamp tutorial. I have checked my code to make sure it's free of errors. admin is working fine but other urls are not.
Here's urls.py code from the main application directory:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Here's urls.py from the votings app directory:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index, name='index'),
path('<int:question_id>/',views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
I am using django 2.0.5.
Thanks
Unless you've made a mistake copying the wrong urls.py for votings app, the problem has to be it.
This is the main urls.py of your project:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
FYI, according to the docs include() adds urls from your app directory's (in your case it's voting) urls.py to the main urls.py (in memory). This keeps the main urls.py from getting too big to read.
And this is the urls.py of your votings app which is literally the copy of main urls.py:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Don't you see any problem here? There's no endpoint. Where's the associated view (function-based or class based) for this url?
I suggest writing a view in your views.py and test it out:
Votings app views.py:
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
Votings app urls.py:
from django.urls import include, path
from . import views
urlpatterns = [
path('home/', views.current_datetime, name='home'),
]

Adding a sitemap to Django 1.10.7

I'm having issues getting the Django setup to generate a sitemap for me.
I have added the following to my settings file
'django.contrib.sites',
'django.contrib.sitemaps',
and in my urls file I have the following:
from django.conf.urls import include, url
from django.contrib import admin
from ames import views
from cms.sitemaps import CMSSitemap
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^contact/', include('contact.urls')),
url(r'^news/', include('news.urls')),
url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^$', views.home),
url(r'^', include('cms.urls')),
]
When deploying these amends I get the following error on the site:
TypeError at /sitemap.xml/
view must be a callable or a list/tuple in the case of include().
Any thoughts would be most welcome.
Amended urls.py file:
from django.conf.urls import include, url
from django.contrib import admin
from cms.sitemaps import CMSSitemap
from django.contrib.sitemaps.views import sitemap
from ames import views
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^contact/', include('contact.urls')),
url(r'^news/', include('news.urls')),
url(r'^sitemap.xml$', sitemap, {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^$', views.home),
url(r'^', include('cms.urls')),
]
try it:
from django.contrib.sitemaps.views import sitemap
# you code
url(r'^sitemap.xml$', sitemap, {'sitemaps': {'cmspages': CMSSitemap}}),
and remove
url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
all information for the solution is in error view must be a callable or a list/tuple in the case of include()

ImportError: No module named urls

I have a django rest project which is built on Django1.7. I need to run it on Django 1.11. When i run
python manage.py migrate
The error is:
ImportError: No module named urls
on url.py line
url(r'^docs/', include('rest_framework_swagger.urls')),
I have already made modifications in url.py file to avoid patterns. The url.py file look like
from django.conf.urls import include,url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic.base import TemplateView
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^grappelli/', include('grappelli.urls')),
url(r'^docs/', include('rest_framework_swagger.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'', include('gcm.urls')),
url(r'^', include('apps.account.urls')),
url(r'^', include('apps.vegetables.urls')),
url(r'^', include('apps.orders.urls')),
url(r'^', include('apps.listings.urls')),
url(r'^', include('apps.rating.urls')),
url(r'^', include('apps.faq.urls')),
url(r'^thank-you/$', TemplateView.as_view(template_name="thankyou.html"), name="thankyou"),
url(r'^/error/$', TemplateView.as_view(template_name="error.html"), name="error"),
url(r'^$', TemplateView.as_view(template_name="index.html"), name="home"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
'',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT})]
How could i run it?
In urls.py add this at the bottom,
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT
From Django 1.9 it does not support to add the URLs as a string, but it need to be imported as callable. So remove this from your urls.py.
urlpatterns += [ '', (r'^static/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT})]

Adding django-directupload to Django project

I was looking to try django-directupload in one of my Django projects, but I'm new to Django and having some trouble with installation. I'm following the installation instructions in the README. I installed django-directupload using sudo pip install django-directupload.
Here is my urls.py, which I don't think is right:
from django.conf.urls.defaults import patterns, include, url
from testproject import settings
import directupload
from django.contrib import admin
directupload.admin.patch_admin()
admin.autodiscover()
urlpatterns = patterns('testproject.database.views',
url(r'^(\d+)/$', 'test_view', name="test_page"),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^directupload/', include(directupload.urls))
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
Edit:
So I've gained some insight into overriding admin templates (Thanks ilvar for the link). I copied and pasted the contrib/admin/templates/admin/change_form.html file into the project templates directory in the /admin/nameofmyapp/ subdirectory, and added {% load directupload_tags %}{% directupload_head %} below the rest of the load tags. When I go to the Django admin I get the exception: 'module' object has no attribute 'admin' on line 6 of urls.py.
https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#overriding-admin-templates
urls.py should look like this:
from django.conf.urls.defaults import patterns, include, url
from testproject import settings
from directupload.admin import patch_admin
from django.contrib import admin
patch_admin()
admin.autodiscover()
urlpatterns = patterns('testproject.database.views',
url(r'^(\d+)/$', 'test_view', name="test_page"),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^directupload/', include(directupload.urls))
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
My imports were incorrect.

how to include urlpatterns in django?

In my project I have one app which have its own urls.py like this
urlpatterns = patterns('',
(r'^(?P<language>\w+)/$', 'MainSite.views.home_page'),)
(above file is in my application )
I am trying include this file in main(project's) urls.py
like this :
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'', include('myproject.MainSite.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG :
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
but after this I can able to call the MainSite's (app's) view but my admin url is not working
I tried
urlpatterns = patterns('',
(r'^$', include('myproject.MainSite.urls')),
url(r'^admin/', include(admin.site.urls)),
)
but after this this makes admin work but my app's view won't get called,
how do I solve this.
You're including your views at the root level. Since it comes before the urlpattern for the admin, the first urlpattern catches everything, so nothing is ever passed to the admin views.
The simplest fix is to simply reverse the order:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'', include('myproject.MainSite.urls')),
)
Then, your views will only catch anything the admin doesn't.