How to configure Django VPS location in URL request - django

I am trying to configure my Django app (Djnago 3.05) to work from a server location.
The location is http://dev.apps.net/tac.
How do i include this in each request?
I managed to do this if adding in views.py the '/tac' prefix and add a folder named 'tac' in which all the templates exist.
myviews.py
#login_required
def item_list(request):
meta = "V1.1"
if not any(field in request.GET for field in set(UserFilter.get_fields())):
user_list = item.objects.order_by("-date").all()[:50]
else:
user_list = item.objects.order_by("-date").all()
user_filter = UserFilter(request.GET, queryset=user_list)
return render(request, "tac/items/item_list.html", {"filter": user_filter})
urls.py
urlpatterns = [
url(
r"^login/$",
admin_views.LoginView.as_view(template_name="registration/login.html"),
name="login",
),
url(r"^$", TemplateView.as_view(template_name="home.html"), name="home"),
url(r"^input/$", views.inputprocess, name="inputprocess"),
url(r"^items/$", views.item_list, name="item_list"),
url(r"^items/create/$", views.item_create, name="item_create"),
url(r"^items/(?P<pk>\d+)/update/$", views.item_update, name="item_update"),
url(r"^items/(?P<pk>\d+)/delete/$", views.item_delete, name="item_delete"),
url(r"^reports/$", views.reports, name="reports"),
url(r"^timeline/$", views.timeline, name="timeline"),
url(r"^support/$", views.support, name="support"),
url(r"^dbToools/$", views.dbTools, name="dbTools"),
url(r"^logout/$", views.logout_view, name="logout_view"),
url(r"^upload/$", views.upload, name="upload"),
path("accounts/login/", auth_views.LoginView.as_view()),
path("admin/", admin.site.urls),
]
I am not sure if this is the correct way. How could I do this more efficient? Could you guys please help me here?
Thanks!

I managed to solve this using this config for urls.py (i found it in another post here)
urlpatterns = [
url(
r"^tac/",
include(
[
url(
r"^login/$",
admin_views.LoginView.as_view(
template_name="registration/login.html"
),
name="login",
),
url(
r"^$", TemplateView.as_view(template_name="home.html"), name="home"
),
url(r"^input/$", views.inputprocess, name="inputprocess"),
url(r"^items/$", views.item_list, name="item_list"),
url(r"^items/create/$", views.item_create, name="item_create"),
url(
r"^items/(?P<pk>\d+)/update/$",
views.item_update,
name="item_update",
),
url(
r"^items/(?P<pk>\d+)/delete/$",
views.item_delete,
name="item_delete",
),
url(r"^reports/$", views.reports, name="reports"),
url(r"^timeline/$", views.timeline, name="timeline"),
url(r"^support/$", views.support, name="support"),
url(r"^dbToools/$", views.dbTools, name="dbTools"),
url(r"^logout/$", views.logout_view, name="logout_view"),
url(r"^upload/$", views.upload, name="upload"),
path("accounts/login/", auth_views.LoginView.as_view()),
path("admin/", admin.site.urls),
]
),
),
]

Related

drf-yasg doesn't include the "api/" portion of the urls

I'm using drf-yasg to generate a Swagger schema, but it removes the "api/" portion of the url.
schema_view = get_schema_view(
openapi.Info(
title="My API",
default_version='v1',
description="...",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="hello#mycompany.com"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=[permissions.AllowAny],
)
router = routers.DefaultRouter()
router.register(r'spaces', SpacesViewSet, basename='spaces')
urlpatterns = [
url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
path('api/', include(router.urls)),
path('api/search-options', SearchPlacesOptionsView.as_view()),
]
result on /swagger
As you can see for the routes from the drf router, it doesn't include the /api portion of the url. However for the regular api/search-options endpoint it removes the /api portion as well, so I don't think it has something to do with the router.

Django URLs Reverse working but not updating my URL

I'm new to Django 4.0.4.
I'm trying to use reverse in model to dynamically change the url without affecting other branch not affecting.
url.py:
urlpatterns = [
path('', home_view, name='home'),
path('products/', product_list, name='product_list'),
path('products/<int:myid>/', dynamic_lookup_view, name='product-detail'),
path('admin/', admin.site.urls),
]
models.py
def get_absolute_url(self):
return reverse("product-detail", kwargs={"myid": self.id})
html
<p>
{{instance.id}} {{instance.title}}
</p>
Output(working):
enter image description here
enter image description here
Problem:
when i change root url for dynamic_lookup_view from 'products/int:myid/' to 'ps/int:myid/' in url.py
path('products/', product_list, name='product_list'),
path('p/<int:myid>/', dynamic_lookup_view, name='product-detail'),
There is no update in my instance.get_absolute_url in my html!?

Django auth_views.PasswordResetView not sending custom email template

I'm trying to send a custom email template with the reset token. But it keeps sending raw HTML no matter what I do. I have previous experience of doing exactly the same thing without any problem with the Django and Django rest framework.
Here's how I did it,
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls')),
path('', include('django.contrib.auth.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
store/urls.py
...
path('password_reset',
auth_views.PasswordResetView.as_view(
template_name='registration/password_reset_form.html',
html_email_template_name='registration/html_password_reset_email.html',
email_template_name = 'registration/password_reset_email.html',
),
name='password_reset'
),
folder structure
...
templates
|-- registration
|-- html_password_reset_email.html
|-- password_reset_email.html
|-- ...
...
Note:
I tried deleting the password_reset_email.html template after that it sends the default Django email template.
Hoping for any guidance to solve the issue thanks in advance.
Finally days(3) of debugging I found my dumb mistake :(,
I was including all auth urls path('', include('django.contrib.auth.urls')) in my main urls.py file and re defining the auth.urls again in store/urls.py differently so the overriding doesn't happen correctly.
So, instead of,
path('password_reset', # <<< here
auth_views.PasswordResetView.as_view(
...
),
name='password_reset'
),
it should be,
path('password_reset/', # <<< here
auth_views.PasswordResetView.as_view(
),
name='password_reset'
),
Not gonna delete the question (^.^') #for_sake_of_dumb_fellows_like_me

Django urldispatcher: directing the request to special view

Django=3.0.8
urls.py
urlpatterns += [
path('<slug:categories>/', include('categories.urls', namespace="categories")),
]
categories/urls.py
urlpatterns = [
path('', CategoryGeneralView.as_view(), name='general'),
re_path(r'^(?P<type>novosti|tema)$/',CategorySpecialView.as_view(), name="type"),
path('draft/<slug:slug>/', PostDetailView.as_view(), name="draft_post_detail"),
path('<slug:slug>/', PostDetailView.as_view(), name="post_detail"),
]
Problem
When I input either of
http://localhost:8000/windows/tema/
http://localhost:8000/windows/novosti/
the request goes to PostDetailView. But I want it to go to CategorySpecialView.
How can I achieve this?
Why not just use 2 separate paths, i.e.:
path('novosti/', CategorySpecialView.as_view(), name = 'type'),
path('tema/', CategorySpecialView.as_view(), name = 'type'),

How to change root URL configuration in order to use a namespace for the user URLs

Site-Wide URL:
from user import urls as user_urls
app_name='user'
urlpatterns = [
re_path(r'^user/',include(user_urls)),
]
Since the admin app, also defines URL patterns named login and logout in django/contrib/admin/sites.py. I need Django pointing to user app.
It's still pointing towards registration/login.html (i.e admin app). I tried namespacing but its been removed in Django 2.0.
user/urls.py :
urlpatterns = [
path(r'',RedirectView.as_view()),
re_path(
r'^login/$',auth_views.LoginView.as_view(template_name='user/login.html'),
name='login'
),
re_path(
r'^logout/$',auth_views.LogoutView.as_view(template_name='user/logged_out.html')
,
{
'extra_context':{'form':AuthenticationForm }
}, name='logout'
),
]
In order to access the URLs by namespace in django 2 you need to move you app_name attribute so user/urls.py would become;
app_name = 'user'
urlpatterns = [
path(r'', RedirectView.as_view()),
re_path(
r'^login/$',auth_views.LoginView.as_view(),
{'template_name':'user/login.html'},
name='login'
),
re_path(
r'^logout/$',auth_views.LogoutView.as_view(),
{
'template_name':'user/logged_out.html',
'extra_context':{'form':AuthenticationForm }
},
name='logout'
),
]
The URLs defined in users.urls will have an application namespace of user.
Alternatively you could namespace URLs in the same file by doing;
user_patterns = ([
path(r'', RedirectView.as_view()),
re_path(
r'^login/$',auth_views.LoginView.as_view(),
{'template_name':'user/login.html'},
name='login'
),
re_path(
r'^logout/$',auth_views.LogoutView.as_view(),
{
'template_name':'user/logged_out.html',
'extra_context':{'form':AuthenticationForm }
},
name='logout'
),
], 'user')
urlpatterns = [
re_path(r'^user/', include(user_patterns)),
]
The docs on this can be found here; https://docs.djangoproject.com/en/2.0/topics/http/urls/#url-namespaces-and-included-urlconfs