in your main app urls.py
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
# If you are using admin
path('admin/', admin.site.urls),
path(
"robots.txt",
TemplateView.as_view(template_name="robots.txt", content_type="text/plain"),
),
path(
"sitemap.xml",
TemplateView.as_view(template_name="sitemap.xml", content_type="text/xml"),
),
]
Then go to your template root folder and create a robots.txt file and you can add something like this
User-Agent: *
Disallow: /private/
Disallow: /junk/
Got to your tempalte root folder again and create another file sitemap.xml
and you can add somemthing like this or get it done properly with sitemaps generator here is an example:
<url>
<loc>https://examplemysite.com</loc>
<lastmod>2020-02-01T15:19:02+00:00</lastmod>
<priority>1.00</priority>
</url>
Now if you run python manage.py runserver you can test it 127.0.0.1:8000/sitemap.xml
and /robots.txt
and it will work. But this won't work in your production server because you will need to let nginx know about this and give the paths.
So you will need to ssh into your server and for example in nginx you should have a configuration file that you named when you built it. You should cd into /etc/nginx/sites-available
in that folder you should have the default file (which you should leave alone) and there should be another file there that you named, usually should be named same as your project name or website name. Open that file with nano but take a back up first. Next you can add your paths for both files like this:
Be aware of the paths, but obviously you can look at the file and you should get an idea you should see the path to static file or media. So you could do something like this.
location /robots.txt {
root /home/myap-admin/projects/mywebsitename/templates;
}
location /sitemap.xml {
root /home/myap-admin/projects/mywebsitename/templates;
}
/home/myap-admin/projects/mywebsitename/templates
you should know the path to your mywebsitename. This is just an example path that leads to templates folder.
Make sure you then run service nginx restart