Implementing RAML documentation in django project - python

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

Related

importing apps into urls.py

I am trying to import views from my apps into the urls.py file. For awhile, I was able to using "from app_name.views import view_name", but for some reason now it is not recognizing the app name.
I did not change anything in my settings.py file so I'm not sure what caused this. I did delete some migrations and an my database.
One thing that is weird is that I can import them if I go up a folder so, "from src.appname.views" and this seems to work, but is not what I want since by base directory is the src folder.
from django.contrib import admin
from django.urls import path
from pages.views import home_page, history, more_information, prop_analysis
from Product.views import property_analysis_tools, property_analysis_results
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_page),
path('home', home_page),
path('history', history),
path('information', more_information),
path('analyze', prop_analysis),
path('results', property_analysis_results)
Another odd thing is that when I run the server, all of these views seem to work. Can Django sometimes give off false errors?
In Django in order to import an app.view into project urls, we create an additional urls.py in every app and link that urls.py into
our project urls.py , just as shown below
change project urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls', namespace="pages")),
]
create another urls.py in your pages app
as below, similarly in any other apps.
from django.urls import path
from .views import (home_page,history,....)
app_name = 'pages'
urlpatterns = [
path(' ', homepage , name='homepage-view'),
path('history/',history, name='history-view'),
]
Every other view from respective app.views will be included in corresponding app.urls [if u have multiple apps]
nB: changes may occur according to your settings.py configurations,
for more, read this django URL documentation
watch Corey Schafer playlist best Django tutorial for beginners

I'm getting 404 error on every page in django

I'm developing my first app in Django and facing some issues. The server runs smoothly and I can operate the admin panel without any issues.
However, all of the app pages including the default homepage show a "404 not found" error.
I have created a templates directory in project root
updated this line in settings.py for templates,
'DIRS': [os.path.join(BASE_DIR,'templates')],
View for the app
from django.shortcuts import render
from .models import *
# Create your views here.
def customers(request):
customers = Customer.objects.all()
return render(request, "customers.html", {'customers': customers})
urls for the app
from django.urls import path from . import views
urlpatterns = [path('customers',views.customers, name='customers')]
urls for the project
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('trips/',include('trips.urls')),
path('customers/',include('customers.urls')),
path('drivers/',include('drivers.urls')),
path('vehicles/',include('vehicles.urls')),
path('admin/', admin.site.urls), ]
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Help is appreciated
I can't see any problem with your case, you should be able to load http://localhost:8000/customers/customers. Maybe double customers was not you intention, then remove one of them (one in main urls.py one in app's).
Also when Django debug mode is active and you get a 404 because of URL mismatch, it shows you a list of URLs you have. To see it navigate to a non existing URL like http://localhost:8000/aaa. Then if you see customers/ there try http://localhost:8000/customers/. Go step by step to find the problem.

django: Cant open two different pages when I have two views for an application

I am doing django course from udemy, i did one experiment. Below is my folder structure
Project
appTwo
urls.py
ProTwo
urls.py
appTwo/urls.py
from django.conf.urls import url
from appTwo import views
urlpatterns = [
url(r'^$',views.help,name='help'),
url(r'^$',views.users,name='users'),
]
ProTwo/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from appTwo import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^help/',include('appTwo.urls')),
url(r'^users/',include('appTwo.urls')),
path('admin/', admin.site.urls),
]
Now when i try to open the page users by http://127.0.0.1:8000/users it opens the page help.html. For http://127.0.0.1:8000/help it opens help page. When I comment the first entry in urlpatterns in urls.py it opens the users page even if i try to open help page. Can anyone please guide me what wrong I am doing or its working as expected.
you need to use different patterns for each View:
urlpatterns = [
url(r'^help$', views.help, name='help'),
url(r'^users$', views.users, name='users'),
]
the name attribute is only useful for the concept of reversing, maybe further in your course?
also, the r'' strings in python are regular expressions, you might want to learn more about them.
I got the answer for this. Below two files need to be changed
appTwo/urls.py
from django.conf.urls import url
from appTwo import views
urlpatterns = [
url(r'^help$',views.help,name='help'),
url(r'^users$',views.users,name='users'),
]
ProTwo/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from appTwo import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^appTwo/',include('appTwo.urls')),
path('admin/', admin.site.urls),
]
Explanation:
When I type in browser "basic url" i mean the address here it is http://127.0.0.1:8000/, it will go to ProTwo/urls.py which is the project folder. This will open the index page as per line url(r'^$',views.index,name='index'). If you need two configure two different page, give url(r'^appTwo/',include('appTwo.urls')), in the ProTwo/urls.py. This will call appTwo/urls.py. Now for help type http://127.0.0.1:8000/appTwo/help and for users type http://127.0.0.1:8000/appTwo/users.

Django - importing view from Dependency

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

django rest framework api documentation through swagger

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.

Categories

Resources