how Video sitemaps and video sitemap alternatives for django - django

The sitemap framework
Django comes with a high-level sitemap-generating framework to create sitemap XML files.
Overview
A sitemap is an XML file on your website that tells search-engine indexers how frequently your pages change and how “important” certain pages are in relation to other pages on your site. This information helps search engines index your site.
The Django sitemap framework automates the creation of this XML file by letting you express this information in Python code.
how use Django The sitemap framework for videos
don't work
from django.urls import path
from django.conf.urls import url
from . import views
from django.contrib.sitemaps.views import sitemap
from .sitemaps import PostSitemap
sitemaps = {
'posts': PostSitemap
}
app_name = 'blog'
urlpatterns = [
path('', views.home, name='homepage'),
path('search/', views.post_search, name='post_search'),
path('articles/<slug:post>/', views.post_single, name='post_single'),
path('videos', views.videos, name='videos'),
path('video/<slug:post>/', views.post_single, name='video_single'),
# path('category/<category>/', views.CatListView.as_view(), name='category'),
url(r'^a/(?P<hierarchy>.+)/$', views.show_category, name='category'),
# url(r'^(?P<slug>[\w-]+)/$', views.post_detail, name="detail"),
path('bodyOrgans/<bodyOrgans>/', views.bodyOrgans.as_view(), name='bodyOrgans'),
path('page/<page>/', views.page.as_view(), name='page'),
path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='sitemap'),
path('custom-sitemap.xml', views.index, {
'sitemaps': sitemaps,
'template_name': 'custom_sitemap.html'
}),
]
how make output like it
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url>
<loc>http://www.example.com/videos/some_video_landing_page.html</loc>
<video:video>
<video:thumbnail_loc>http://www.example.com/thumbs/123.jpg</video:thumbnail_loc>
<video:title>Grilling steaks for summer</video:title>
<video:description>Alkis shows you how to get perfectly done steaks every
time</video:description>
<video:content_loc>
http://streamserver.example.com/video123.mp4</video:content_loc>
<video:player_loc>
http://www.example.com/videoplayer.php?video=123</video:player_loc>
<video:duration>600</video:duration>
<video:expiration_date>2021-11-05T19:20:30+08:00</video:expiration_date>
<video:rating>4.2</video:rating>
<video:view_count>12345</video:view_count>
<video:publication_date>2007-11-05T19:20:30+08:00</video:publication_date>
<video:family_friendly>yes</video:family_friendly>
<video:restriction relationship="allow">IE GB US CA</video:restriction>
<video:price currency="EUR">1.99</video:price>
<video:requires_subscription>yes</video:requires_subscription>
<video:uploader
info="http://www.example.com/users/grillymcgrillerson">GrillyMcGrillerson
</video:uploader>
<video:live>no</video:live>
</video:video>
</url>
</urlset>

Related

How do you implement Django namespaces

I have started my journey with Django and I am thoroughly loving it. I do have a question about namespaces which I hope someone will be able to help me with and explain why it works like that.
I understand namespaces are used to make sure that if you have two pages with the same name that the url and reverse function points to the right page. I have been trying to implement namespaces in a test app I am writing but have not been able to do it.
Here is what I have so far (This is without namespaces as I haven't been able to get it to work.
Extract from app urls.py
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', views.index, name = "index"),
]
project urls.py
from django.contrib import admin
from django.urls import path, include, re_path
import gallery
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('index/', include('gallery.urls')),
]
And lastly, this is my view.py file in my app folder
from django.shortcuts import render
from django.urls import reverse
# Create your views here.
def index(request):
return render(request,"gallery/index.html")
Any help will be appreciated
URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It’s a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed. In other words, since multiple instances of a single application will share named URLs, namespaces provide a way to tell these named URLs apart. See URL namespaces
In your case:
project urls.py
from django.contrib import admin
from django.urls import path, include, re_path
import gallery
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls', namespace='your-namespace')),
path('index/', include('gallery.urls',namespace='your-namespace')),
]
Extract from app urls.py
from django.urls import path, re_path
from . import views
app_name = 'app'
urlpatterns = [
path('', views.index, name = "index"),
]
in the template:
{% url 'app:index' %}

Django url can't work after i move it down from the list

I am making a blog app using Django. In my blog app, i have 6 paths. This is my app urls.py
from . import views
from .views import AddPostView
from django.urls import path
from .feeds import LatestPostsFeed
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
# path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('upload/', views.image_upload_view, name="upload"),
path('add_post/', AddPostView.as_view(), name='add_post'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path("feed/rss", LatestPostsFeed(), name="post_feed"),
]
The app is working perfectly, but whenever i try to move the path('<slug:slug>/', views.post_detail, name='post_detail') up from the add_post url (code below) and try to open add_post
from . import views
from .views import AddPostView
from django.urls import path
from .feeds import LatestPostsFeed
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
# path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('upload/', views.image_upload_view, name="upload"),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path('add_post/', AddPostView.as_view(), name='add_post'),
path("feed/rss", LatestPostsFeed(), name="post_feed"),
]
suddenly i have an error saying :
Page not found (404)
No Post matches the given query.
Request Method: GET Request URL: http://localhost:8000/add_post/
Raised by: blog.views.post_detail
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
admin/
[name='home']
upload/ [name='upload']
<slug:slug>/ [name='post_detail']
The current path, add_post/, matched the last one.
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.
I don't understand why it happens and i am curious and want to know what has just happened. maybe there are some concepts about django urls that i don't know. Can you explain to me what happen in this error?

How to fix "error path not found" in django rest framework

I am trying to build an API of my blogging website using Django rest framework, but my URL is not matching.
I am trying Django Rest framework for the first time so I am not quite able to fix this. But I think I mess this up in url_patterns.
Here is my URL code from the main directory(the directory which contains settings.py) .
`
from django.conf.urls import url,include
from django.contrib import admin
from django.urls import path, include
from blog import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'apipost',views.PostViewSet)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('blog.urls')),
path('api-auth/',include('rest_framework.urls',namespace='rest_framework')),
]
`
I am trying url http://127.0.0.1:8000/apipost and expect to get value in json format.
You need to add router.urls to your urlpatterns.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('blog.urls')),
path('api-auth/',include('rest_framework.urls',namespace='rest_framework')),
]
urlpatterns += router.urls
Django REST Framework Won't magically register your router in urlpatterns, you have to do it by yourself. You can use urlpatterns += router.urls if you want to add them to the root of your urlpatterns, or url(r'^api/', include((router.urls, 'app_name'))), if you want to set subpath for them.

How to navigate from current html page to another html page in django

I'm working on django app and i'm facing a problem when i need to navigate from my index.html page to another about.html page. I'm setting everything in this way below:
urls.py (myproject)
from django.urls import path, include
urlpatterns = [
path('', include('weather_app.urls')),
path('about/', include('weather_app.urls'))
]
urls.py (myapp)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name="about")
]
In index.html, everything is working well, so i will not put the code.
and in view.py:
from django.shortcuts import render
import requests
def about(request):
return render(request, "about.html")
i have the code below in my index.html:
About
and i cannot get about.html page when click About link as you can see above.
Can anybody help me?
Thanks in advance..
You have to use this tag to avoid manual rendering of urls.
In your code:
About
Let me know if this works!

Django views in project directory

I have a project with two apps with their respective views, but I want to create a views.py in django project directory for some generic pages such as about, login... It is correct to place the views.py and templates in the root project folder for this purpose?
I have been reading about this. The practice more recommended for this is create an app that creates cohesion between other apps, call it web_site or site whatever is better for you.
python manage.py startapp my_site
Everything in this site app will be done the regular way.
In the projects url you will want to import the url in this way so the web pages appear in the / url pattern.
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_site.urls'))
]
I do this for project level pages like you describe. These are usually simple pages that tie the project's apps together, and not contain any business logic.
You can do that by making views.py file into the project directory.
from django.contrib import admin
from django.urls import path, include
from attendance import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('insert.urls')),
path('login/', views.logins, name='login'),# relevant line
]
And this is the code of login view
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def logins(request):
return HttpResponse("THIS IS LOGIN PAGE !!!")