How to create a link that takes me to my DjangoRest api - django

I started studying Django a few days ago and i want that when I click on a button it takes me to the url of my api.
This is my urls from my app:
from django.urls import path, include
from animes import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register('animes', views.AnimesViewSet, basename='animes')
router.register('nota', views.NotasViewSet, basename='notas')
urlpatterns = [
...
path('api/', include(router.urls), name='api')
]
the problem from my template is here:
<a class="botao__link" href="{% url 'api' %}"><button class="botao__botao" type="submit">API</button></a>
when i write the url manually i can go to the api without problems , but I can't go clicking the button

I'm not pretty sure but I think removing the type="submit" attribute should help

After several attempts I solved the problem by passing an absolute url

Related

Is there a way I could send a book primary key to a url in another django app?

I want to make a book marketing web app using Django and I want to show the book details using another Django app, and I need the book primary key in the details app. my question is what is the best way to pass the book pk to the detail URL defined in the book_details app.
this is my home urls.py:
from django.urls import path, include
from .views import *
urlpatterns = [
path('', home_page_view, name='home'),
path('books/', include('book_details.urls'), name='books'),
]
and this is my details app (book_details) urls.py:
from django.urls import path, include
from .views import *
app_name = 'book_details'
urlpatterns = [
path('<int:pk>/', book_details_view, name="book_detail")
]
I tried doing it like this in the home app template and it does not work:
<a href="{% url 'book_details:book_detail' post.pk %} " class="book-card-link">
the Error:
Reverse for 'book_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['books/(?P[0-9]+)/\Z']
Sorry if my question is not very clear, it's my first time asking a question here, Thanks.

How to redirect from a view in one app to a view in another?

I am trying to build a job-board type of website. Right now I have user authentication (login, logout, register, password change, etc) in one app (account) and the job model and all other views/templates in another app(job).
To have the user log in, I used the views found in django.contrib.auth(this is account/urls.py):
from django.urls import path
from django.contrib.auth import views as auth_views
urlpatterns = [
path('login/', auth_views.LoginView.as_view(), name = 'login'),
]
In the job app I have created a urls.py file:
from django.urls import path
from . import views
urlpatterns = [
path('dashboard/', views.dashboard, name = 'dashboard'),
]
Upon logging in, I would like the user to be redirected to this dashboard URL/view found in the job app. How can I do this? I know that if I just put the dashboard view/url into the account app all I would need to do is add this to settings.py:
LOGIN_REDIRECT_URL = 'dashboard'
But, how can I redirect to a view in another app?
Final note: I separated this into two apps in the first place because I've read that is good practice, but am not sure if it's needed here.
Test this:
LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
in settings.py:
LOGIN_REDIRECT_URL = reverse_lazy('job:dashboard')
This only works for redirection once logged in.
[settings.py]:
LOGIN_REDIRECT_URL = 'dashboard'
If you want to redirect urls from any app to another, you must use static tags.
[file.html]
{% load static %}
and then:
<a href="{% url 'dashboard' %}">

How do create url to django Admin within my user page (HTML)

I'm new to coding, I'm trying to create URL link in NavBar to redirect my Django Admin exmple:
<a class="nav-link" href="{% url 'admin'%}"> Admin site </a>
However, Django do find; No-Reverse-Match,
It works for other URL links
Things I had to try:
Is adding a name in project URL
including URL path in my App URL
My project URL code:
from Django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(r'admin/', admin.site.urls, name='admin' ),
path(r'accounts/', include('django.contrib.auth.urls')),
path(r'api-auth/', include('rest_framework.urls'), name='rest_framework'),
path(r'', include('app.urls')),
My App URL code:
from django.urls import path, include
from . import views #function views
from django.contrib.auth.decorators import login_required, permission_required
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'tank', views.TankViewSet)
router.register(r'room', views.RoomViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path(r'',login_required(views.index), name='index'),
path (r'test/',views.Test),
path(r'^Water/',login_required(views.Lquid), name='tank'),
path(r'^Ambient/',login_required(views.Ambient), name='room'),
path(r'Rest-api/', include(router.urls)),
#path(r'admin/', admin.site.urls, name='admin' ), this somthing I had try
]
Basically, Django makes in a way that works and redirect to the URL link on any page
NAVBAR:
Admin site
App URL:
path(r'',login_required(views.index), name='index'),
With this two link it will redricet to the page you set.
Apprentice if you cold help
You need to specify the admin page you want to redirect to when doing a reverse lookup of admin. To get to the admin homepage you will want this reverse lookup
reverse('admin:index')
So to link to the admin homepage in your nav bar, it should look like this
<a class="nav-link" href="{% url 'admin:index' %}"> Admin site </a>
Check out the docs on how to reverse lookup admin pages

Django Rest 'api-docs' is not a registered namespace

I'm setting the docs for my api and when I ask for (http://localhost:8000/api/v0/docs/) The app show me this error:
NoReverseMatch at /api/v0/docs/
'api-docs' is not a registered namespace
when try to do this
<script src="{% url **'api-docs:schema-js'** %}"></script>
this is my urls.py file:
from django.contrib import admin
from django.conf.urls import url, include
from django.contrib.auth.models import User
from . import views
from rest_framework import routers, serializers, viewsets
from rest_framework.documentation import include_docs_urls
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'goals', views.GoalViewSet)
urlpatterns = [
url(r'^goals/$', include(router.urls)),
url(r'^docs/', include_docs_urls(title='My API title')),
]
I added the namespace to include_docs_urls but it doesn't work. (I have installed coreapi, pygments and markdown).
Beforehand thanks for helping me.
You should move your docs url pattern into your main urls.py file. This is still a problem in DRF, there is also an issue opened on the official GitHub source: https://github.com/encode/django-rest-framework/issues/4984
The official developer of Django REST Framework, Tom Christie, says:
It's important enough that I'd like to see it highly prioritized, but there's a lot of other things going on at the moment too. Even after we release 3.6.3, there's still also all the work towards 3.7.0's realtime integration.
So your docs pattern should not have any prefixes (like api/v0/).
In conclusion, change your main urls.py:
url(r'^docs/', include_docs_urls(title='API Title'))

Django url configuration error

so I have created a Django application with three html pages. I want from a page which is not the homepage to another. However, the there seems to be some mix up in the url when the server tries to access the latter page.
Here's my application urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.homepage,name='homepage'),
url(r'^findnow.html/$',views.findnow,name='findnow'),
url(r'^more.html/$',views.more,name='more')
]
I wish to go from "findnow.html" to "more.html". I want the url to be "localhost:port/more" but instead the server goes to "localhost:port/findnow.html/more.html".
Here's my html code snippet for findnow:
<body>
<div id="googleMap" style="width:500px;height:380px;text-align:center;"></div>
MORE
</body>
</html>
Here's my views.py:
def more(request):
return render(request,'myapp/more.html')
So adjust it to be:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.homepage,name='homepage'),
url(r'^findnow/$',views.findnow,name='findnow'),
url(r'^more/$',views.more,name='more')
]
And your template:
<body>
<div id="googleMap" style="width:500px;height:380px;text-align:center;"></div>
MORE
</body>
</html>
This is happening because you are linking more.html as a relative page.
You should either prepend a / to it:
MORE
Or reverse it using the url tag instead:
MORE