It is already giving the swagger inbuilt page on localhost. It is reading my urls perfectly. But i am unable to define the interior headers, response body. I dont know where to define it and how to connect it with my django project.
urls.py
from django.conf.urls import url, include
from django.contrib import admin
from users import views
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='Pastebin API')
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/users/emailsignup/', views.SignUp.as_view()),
url(r'^api/v1/users/userget/', views.UserDetail.as_view()),
url(r'^$', schema_view),
]
Thus the schema_view is given . But plz expalain me how to define my response body in it with code.
Related
I have 2 django app. user and management. I have added dashboard/ in main app urls which redirects to user app urls. For this we need to login. All urls in user app is: dashboard/ and for login dashboard/login
but it shows error in /login.
ERROR:
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/login
Main app urls.py
from user import urls as user_urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('dashboard/', include(user_urls))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
User app urls.py
from django.urls import path, include
from nh_user import views as users_views
from management import urls as management_urls
urlpatterns = [
path('login', users_views.user_login, name='user-login'),
path('logout', users_views.user_logout, name='user-logout'),
path('dashboard/', include(management_urls)),
path('', users_views.index, name='user-index'),
]
You specified dashboard/ as the route for all the urls in your users app. Django won't look for these urls unless you include '/dashboard/' in your address bar.
Currently the working path is http://localhost:8000/dashboard/login.
If you want to change your login path to http://localhost:8000/login you need to remove 'dashboard/' as the route in your project urls.py.
from user import urls as user_urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(user_urls))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
All the routes for the urls in your users app will now be at directly at http://localhost:8000/route-name without anything preceding them.
I'm trying to use this library since i want to add 2FA Auth to my project. In order to integrate the module in my project, i need to import their views to my urls.py file, right?
I tried to import SetupView, but i'm getting this error: module 'allauth_2fa.views' has no attribute 'homepage'. Here is what i understood: it looks like if i import a view from the dependency, it will only read those views from the dependency but not my own views declared on views.py.
from django.urls import path
from . import views
from django.conf.urls import url, include
from django.conf.urls import url
from allauth_2fa import views
app_name = "main"
urlpatterns = [
path("setup/", views.TwoFactorSetup.as_view(), name="setup"),
path("", views.homepage, name="homepage"),
path("register/", views.register, name="register"),
path("logout/", views.logout_request, name="logout"),
path("login/", views.login_request, name="login"),
]
Extra: SetupView will generate the page required to enable the 2FA authentication, that's why i need it. Later i will also import the other views required to have my two factor authentication fully running
At first you imported
from . import views
And then:
from allauth_2fa import views
And after that you tried to do:
path("", views.homepage, name="homepage"),
And views is allauth_2fa.views not from your project
So you just need to do like this:
from allauth_2fa import views as allauth_2fa_views
And then use it when you need
I already set up RAML for django . Its reading my urls and showing the documentation page in my localhost. Now i need to write the RAML Code for documentation where i define all the requests and responses . I dont know where to write the RAML code in my django project .
urls.py
from django.conf.urls import url, include
from django.contrib import admin
from users import views
from rest_framework.schemas import get_schema_view
from rest_framework_raml.renderers import RAMLRenderer, RAMLDocsRenderer
schema_view = get_schema_view(
title='Example API',
renderer_classes=[RAMLRenderer, RAMLDocsRenderer])
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/users/emailsignup/', views.SignUp.as_view()),
url(r'^api/v1/users/userget/', views.UserDetail.as_view()),
url(r'^api/v1/users/emailsignin/', views.SignIn.as_view()),
url(r'^api/v1/users/emailsignout/', views.SignOut.as_view()),
url(r'^raml/$', schema_view),
]
I need to know where to write the raml code in the project and how to give its url
I'm new on Django REST Framework. I have an index.html where I have simple form for adding items and back-end with Django. I can't understand, how I can connect index.html to Django REST Framework.
I have the next code-files:
models.py
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
category = models.CharField(max_length=100)
def __str__(self):
return self.title
views.py
from django.shortcuts import render
from django.views.generic import TemplateView
from .models import Book
from rest_framework import viewsets
from .serializers import BookSerializer
class Index(TemplateView):
template_name = "index.html"
def get_context_data(self):
context = super(Index, self).get_context_data()
return context
class BookViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Book.objects.all().order_by('title')
serializer_class = BookSerializer
urls.py
from django.conf.urls import url, include
from rest_framework import routers
from myapp import views
router = routers.DefaultRouter()
router.register(r'books', views.BookViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^$', views.Index, name='index'),
]
serializers.py
from .models import Book
from rest_framework import serializers
class BookSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Book
fields = ('title','category')
And if I run localhost:8000/index I get an error 'Page not found'. I can't understand how correct I should include my html pages to the django code. Should I use a router.register for this?
Let's start by defining what django-rest-framework is and what it is not.
It is a nice toolkit to build web APIs. This means that you can define several endpoints (urls) that will handle incoming requests and will return a response in JSON or XML format.
It is not an html rendering tool though. This means that you can't return a response that contains html code to be rendered as a page in the browser. It will return pure json or xml data.
Now, your questions consists of 2 problems:
You can't access the index.html page.
You don't know how to connect this page with the books endpoint.
Regarding problem 1
Check the TEMPLATES settings. Where is your index.html placed? Does django know where to find it? Check out the TEMPLATES setting and assure you have set it up correctly.
Regarding problem 2
Because of the fact that the django-rest-framework endpoint handles incoming requests, you need to generate such a request. But if you will simply access the endpoint in your browser, the page will load/reload and you will see the data from your endpoint in json form on your page.
For your page to remain the same, but at the same time to make a request to your endpoint, you need to use ajax (Asynchronous JavaScript and XML) from within your index.html page. You can emit an ajax request by using one of the following:
With pure JavaScript, by means of XMLHttpRequest class. See this SO question and answers to see how this is made.
If you use jQuery, it has a jQuery.ajax() method to make an ajax request.
If you use any other frontend framework (angular, backbone, ember, etc.), all they have an implementation of ajax calls. Check their documentation for that.
And basically that's it. Good luck!
Could you try putting the api url after your index like this?
urlpatterns = [
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^$', views.Index, name='index'),
url(r'^', include(router.urls)),
]
Or why not use /api/ for your REST Api urls:
urlpatterns = [
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^$', views.Index, name='index'),
url(r'^api/', include(router.urls)),
]
And be sure that you have added 'rest_framework', to INSTALLED_APPS
EDIT:
To access /index/ you must fix your urls.py:
urlpatterns = [
url(r'^index/$', views.Index, name='index'),
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
I have created a page where a user can add a new item (notes in this case) and I am making use of CBV which I have recently started learning.
This is my model form
class NoteForm(forms.ModelForm):
class Meta:
model = Note
fields = ('title', 'note', 'tags')
This is the view in views.py
class NoteCreate(CreateView):
model = Note
form_class = NoteForm
template_name = "add_note.html"
Then this is the url as I used in the urls.py of the app
from django.conf.urls import patterns, url
from . import views
from madNotes.views import NoteCreate, NoteIndex,
urlpatterns = patterns(
'',
url(r'^notes/add/$', NoteCreate.as_view(), name="new_note"),
url(r'^$', NoteIndex.as_view()),
url(r'^(?P<slug>\S+)/$', views.NoteDetail.as_view(), name="entry_detail"),
)
NB: I used the same url as the main page at 127.0.0.1:8000 in the projects urls.py file and it worked.
I have seen several tutorials and even the docs and can't seem to find what I am doing wrong. Will I also need to add a function in order for it to be saved in the db or the CBV will do it all?
EDit: The error I get is this
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/notes/add/
Here is the project's urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from MadNotez import settings
from registration.backends.default.views import RegistrationView
from madNotes.forms import ExRegistrationForm
if settings.DEBUG:
import debug_toolbar
urlpatterns = patterns('',
url(r'^__debug__/', include(debug_toolbar.urls)),
url(r'accounts/register/$', RegistrationView.as_view(form_class = ExRegistrationForm), name='registration_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^admin/', include(admin.site.urls)),
url('^markdown/', include('django_markdown.urls')),
url('^notes/', include('madNotes.urls')),
#url(r'^$', views.NoteCreate.as_view(), name="new note"), when I used it here it worked
)
you say that is the urls.py of the app, which means it is included by the project's urls.py.
As you show now, all the app's URIs go under the notes prefix:
url('^notes/', include('madNotes.urls')),
so as things stand at present the correct URI for the page is
http://127.0.0.1:8000/notes/notes/add/
In order to clean things up a bit, I'd suggest to modify the app's urls.py to
url(r'^add/$', NoteCreate.as_view(), name="new_note"),
so that the page can be reached at
http://127.0.0.1:8000/notes/add/
This way all the app's pages/services are available under the notes prefix and with a simple name that is consistent with their action (i.e. add, delete, etc)