How to make a robots.txt on Django - django

I've seen other answers here, but they aren't really helpful, which is why I am asking. I tried the django-robots framework as well, but it gives me an error when i just put 'robots' in my INSTALLED_APPS
INSTALLED_APPS = [
'index.apps.IndexConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
]

You can use simply use a TemplateView
template: robots.txt
User-agent: *
Disallow: /admin/
Disallow: /accounts/
urls.py
from django.views.generic import TemplateView
urlpatterns = [
# ...
path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type='text/plain')),
# ...
]

Django has nothing to do with your robots.txt file. This file is used as a reference by the search crawlers while going through your website. By default, all paths of your directories are accessible to the crawlers. You can mention the paths inside robots.txt which you don't want the search engines to index. Read more about it here.
You can use your web server(e.g Apache, Ngnix) to render this file.

Related

Django tutorial problem: The current path, polls/, didn’t match any of these

I'm just getting started with Django, and I'm a touch rusty with web development, so this may be an easy one. I'm stepping through the Django Polls Tutorial from the official documentation and I encounter a problem nearly right away. I'm not having success accessing http://localhost:8000/polls/ . I receive the error...
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/polls/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn’t match any of these.
Here is my relevant code...
\mysite\polls\views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
\mysite\polls\urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
\mysite\urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
\mysite\mysite\setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
ROOT_URLCONF = 'mysite.urls'
Development server reads...
Not Found: /polls/
[06/Dec/2022 15:02:01] "GET /polls/ HTTP/1.1" 404 2095
I have tried a hodgepodge of fixes that I've seen from other similar tutorial fixes, but nothing has worked and I feel like I'm taking stabs in the dark at this point.
Thank you Hash & Carlos for taking a look.
I modified as suggested, and restarted server, but I get the exact same result...
\mysite\mysite\settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
I feel as though there is a simple path error occurring somehow. Could there have been an error in my django setup? I do see the spaceship at localhost:8000, as I should.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
you are written the code correclty but you need to add your apps in intalled apps in settings.py file
I deleted my project, and my virtual environment(ok because this was the only thing in it), and started over from scratch. This time I was successful. I'm not completely sure why. It is not clear to me where the problem was, but I am ok moving on to bigger and better challenges. Ty for your responses!
BTW: I did not have to add "polls" to the settings.py file, which is consistent with the tutorial

Adding an aliasName for url name app django

In a django project, I have an app : app_signup.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_signup',
app_signup/urls.py
from django.urls import path, include
from . import views
urlpatterns = [
path('',views.index, name='index'),
]
Is it possible to add an alias to the app in order to see the alias (http://localhost:8000/alias/) instead of the name of the app(http://localhost:8000/app_signup/) in the url on the browser.
The name of the app in INSTALLED_APPS doesn't really matter. Your project's main urls.py file is the one that specifies the path. There when constructing the urlpatterns list, you can do path('alias/', include('app_signup.urls')) instead of path('app_signup/', include('app_signup.urls')).
As far as I understand from the example you added, you are editing the urls.py file within the app you created. Not your app, but directly in the project file (in the same directory as settings.py), urls.py probably as path('app_signup/', include('app_signup.urls')) as mentioned in the answer above. This is the urls.py you need to edit.

Django doesn't see urls.py

I'm new to Django and I'm following this tutorial: https://www.django-rest-framework.org/tutorial/1-serialization/
When I run the server with python manage.py runserver, it seems to work fine showing me no errors.
However when I visit http://127.0.0.1:8000/snippets/ it gives me the following error:
Using the URLconf defined in tutorial.urls, Django tried these URL patterns, in this order:
admin/
The current path, snippets/, didn’t match any of these.
Here's my urls.py located in the tutorial folder:
from django.urls import path, include
urlpatterns = [
path('', include('snippets.urls')),
]
I don't understand what's going on, how come it only checks admin/ if I'm wiring the root urlconf to snippets.urls?
On a related note, when I modify urls.py and add gibberish the server won't give me an error, however if I were to modify models.py then it'll start complaining, that's why I'm getting the feeling it doesn't even check my urls.py and maybe instead some default one...
Any tips are very appreciated!
EDIT:
Here's my urls.py located in the snippets folder:
from django.urls import path
from snippets import views
urlpatterns = [
path('snippets/', views.snippet_list),
path('snippets/<int:pk>/', views.snippet_detail),
]
In settings.py I've only modified the INSTALLED_APPS part with the two last apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'snippets.apps.SnippetsConfig',
]
Are you sure you don't have another instance of Django running?
That's the only thing I can think of since admin isn't even in your current urls

Django-Jet "Page Not Found"

I am attempting to install the Django Jet dashboard, but I'm unable to get anywhere with it.
I've followed the installation instructions, but no matter what I'm getting "Page Not Found" despite the URLs for Jet being listed.
There are a handful of Issues opened on this, but none offer a solution outside that it was resolved in newer versions, and I am using the recommended commit (latest).
Issues:
https://github.com/geex-arts/django-jet/issues/289
https://github.com/geex-arts/django-jet/issues/62
Urls.py:
from django.contrib import admin
from django.urls import path, include, re_path
from depot import views
urlpatterns = [
path('', include('depot.urls')),
path('', include('stores.urls')),
path('admin/', admin.site.urls),
re_path(r'^jet/', include(('jet.urls', 'jet'))),
re_path(r'^jet/dashboard/', include(('jet.dashboard.urls', 'jet-dashboard'))),
]
I have no custom overriding admin templates. I have tried just visiting /admin/, but my admin looks like the default Django Admin (someone mentioned its supposed to take over the original admin templates).
Installed Apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'depot',
'stores',
'jet',
'jet.dashboard',
]
I have also verified I have django.template.context_processors.request under Templates in my settings.py as well.
This is a super important note I missed. Simply moved django.contrib.admin to the bottom of my INSTALLED_APPS and this resolved this issue.
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'depot',
'stores',
'jet',
'jet.dashboard',
'django.contrib.admin',
]

dojango test install results in 404

I made a fresh install of django 1.6.2 in it's own virtualenv. No syncdb, no other configs. The only other apps installed are pip, setuptools and dojango. I followed the instructions in this wiki page https://github.com/klipstein/dojango/wiki/Gettingstarted
when I try 127.0.o.1:8000/dojango/test/ or localhost:8000/dojango/test I get
Page not found (404)
Request Method: GET
Request URL: http://127.0.o.1:8000/dojango/test/
Using the URLconf defined in dojango.urls, Django tried these URL patterns, in this order:
^dojango/ ^dojango/
The current URL, dojango/test/, didn't match any of these.
this is my INSTALLED_APPS
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dojo_tutor',
'dojango',)
and this is my urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^dojango/', include('dojango.urls')),
# url(r'^admin/', include(admin.site.urls)),
)
The wiki page says to use (r'^dojango/', include('dojango.urls')), but I tried both like that and url(r'^dojango/', include('dojango.urls')),. I still get 404
Ok, it was a silly mistake. Foolishly I had named my project dojango so naturally there was a naming conflict. Deleted the project and recreated it with another name. Now it works.