Is it possible to launch Django app with BASEPATH=/api or just BASEPATH=/ based on any configuration?
Based on #narenda-choudhary comment
In file settings.py, where BASEPATH is env variable with relative path
BASEHREF = os.environ.get('BASEPATH', '/')
Then in urls.py
urlpatterns = [
url(r'^{}'.format(settings.BASEHREF), include([
path('admin/', admin.site.urls),
])),
]
Related
I am new to django and I want to set my homepage url
My homepage is 127.0.0.1:8000/home but I want my homepage at 127.0.0.1:8000/ or I want my django start as 127.0.0.1:8000/home as default. How do I do it?
Use empty string in url path instead 'home/' for root url
For example:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app_name.urls'))
]
for urls in your app
urlpatterns = [
path('', views.home, name="home"))
]
After I setup a vue frontend with a django webpack loader, the media files are not rendering.
settings.py
STATIC_URL = '/static/'
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
WEBPACK_LOADER = {
"DEFAULT": {
"BUNDLE_DIR_NAME": "dist/",
"STATS_FILE": os.path.join(BASE_DIR, "frontend", "webpack-stats.json"),
}
}
And urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include("companies.routes.urls")),
path('api/v2/', include("projects.routes.urls")),
path('accounts/register/', RegistrationView.as_view(
form_class=CompanyUserForm,
success_url="/",
), name="django_registration_register"),
path('accounts/', include("django_registration.backends.one_step.urls")),
path('accounts/', include("django.contrib.auth.urls")),
path('api-auth/', include("rest_framework.urls")),
path('api/rest_auth/', include("rest_auth.urls")),
path('api/rest_auth/django_registration/', include("rest_auth.registration.urls")),
re_path(r'^.*$', IndexTemplateView.as_view(), name="entry-point")
]
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]
# adding the media root path.
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
But when I browse the media file url, image isn't rendering. The issue lies inside urlpatterns at this line re_path(r'^.*$', IndexTemplateView.as_view(), name="entry-point") when I comment out this part. I can see the image. But i need the re_path for the frontend view. How should fix this issue?
you need to serve your media files add this to your urls.py
from django.views.static import serve
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]
This is my url.py in WebFetcher
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('Fetcher.urls')),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is my url.py in Fetcher
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name = 'home'),
path('page_objects/', views.page_objects, name = 'page_objects')
]
this is my form
<form action="{% url 'page_objects' %}" method="post" enctype="multipart/form-data">
This is the name of my function in views
def page_objects(request):
I am getting 404 error saying
Using the URLconf defined in WebFetcher.urls, Django tried these URL patterns, in this order:
[name='home']
page_ojects [name='page_objects']
admin/
^media/(?P.*)$
The current path, WebFetcher/page_ojects, didn't match any of these.
I ready all the documentation on the URL Dispatcher and I could not find anything that looks wrong with my code. I hope it is just a syntax error. If you think more of my code will be helpful, comment and I will edit this post.
Edit 1:
I updated my WebFetcher urls.py and my Fetcher urls.py per Daniel's suggest.
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from Fetcher import views
urlpatterns = [
path('WebFetcher/', include('Fetcher.urls')),
path('', views.home, name = 'home'),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.urls import path
from . import views
urlpatterns = [
path('page_objects/', views.page_objects, name = 'page_objects')
]
Now the 404 error I am getting is
Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/WebFetcher/WebFetcher/WebFetcher/page_objects/
Using the URLconf defined in WebFetcher.urls, Django tried these URL patterns, in this order:
WebFetcher/ page_objects/ [name='page_objects']
[name='home']
admin/
^media/(?P.*)$
The current path, WebFetcher/WebFetcher/page_objects/, didn't match any of these.
You need to go to http://mywebsite/page_objects instead of http://mywebsite/WebFetcher/page_objects.
If you want to have the page_objects url nested under WebFetcher then you can do this to your urls.py:
MyProject/urls.py:
urlpatterns = [
path('WebFetcher/', include('Fetcher.urls')), # add the prefix here
path('admin/', admin.site.urls),
]
Fetcher/urls.py:
urlpatterns = [
path('', views.home, name = 'home'),
path('page_objects/', views.page_objects, name = 'page_objects')
]
Note: your home page will now be at http://mywebsite/WebFetcher - if you want it to be at the root, i.e. http://mywebsite then you can do this instead:
MyProject/urls.py:
urlpatterns = [
path('WebFetcher/', include('Fetcher.urls')), # add the prefix here
path('', views.home, name = 'home'), # move the home page path to the root urls.py
path('admin/', admin.site.urls),
]
Fetcher/urls.py:
urlpatterns = [
path('page_objects/', views.page_objects, name = 'page_objects')
]
You have a typo in your urls.py path.
page_ojects should be page_objects
please write this pattern that will work fine.
path('page_ojects/', views.page_objects, name = 'page_objects')
Updated: add this code to your project level urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('Fetcher.urls')),
]
And if it doesn't solve your issue, then there might a issue in your views.py module, maybe your views doesn't find any correct views to dispatch. so write below code for testing purpose,in yours views.py
from django.shortcuts import render
def page_objects(request):
return render("Hello, pages_objects!!")
I am currently using Django REST framework and it works very well for me. The problem is that the API documentation page is public in production and I would like it to only be seen in development (localhost) or by administrators users.
This is my urls.py:
schema_view = get_schema_view(title='My API')
router = DefaultRouter()
# router.register(...)
urlpatterns = [
url(r'', include(router.urls)),
url(r'^docs/', include_docs_urls(title='My API service')),
url(r'^schema/$', schema_view),
]
PS: I'm using the version 3.9.2
You can always check for the DEBUG value and do stuff if it is set (which is set to True in development mode). For example:
from django.conf import settings
urlpatterns = [
url(r'', include(router.urls)),
url(r'^schema/$', schema_view),
]
if settings.DEBUG:
urlpatterns += [
url(r'^docs/', include_docs_urls(title='My API service'))
]
I've read the documentation of the Django about serving static files, and it says you should use django.conf.urls.static.static function in order to server them.
Here is my code:
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from settings import STATIC_ROOT, STATIC_URL
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^api/v1/', include('news.rest_urls', namespace='rest_framework')),
static(STATIC_URL, STATIC_ROOT)
]
And this is the error I receive:
'list' object has no attribute 'regex'
You need to define your STATIC_URL and STATIC_ROOT in settings.py and add to your urlpatterns next:
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)