I'm a beginner of swagger, i have a website written in django(already done), and i want to use swagger to exhibit my apis. I'v read the docs in swagger.io for days, but I still need more help.
I also tried the Django REST Swagger(https://github.com/marcgibbons/django-rest-swagger). I tried the basic example (http://django-rest-swagger.readthedocs.org/en/latest/examples.html#basic-example-with-a-viewset), but it still not work.
Maybe i did something wrong, can anyone help me?
The Django Rest Swagger package is very useful for this.
Did you put 'rest_framework_swagger' into your INSTALLED_APPS setting?
If you did, just put the url(r'^docs/', include('rest_framework_swagger.urls')) in the urls.py where you are exposing your API endpoints and it should display all the other url's that you have in that file.
Related
I want to configure my urls something like these:
username1.mysite.com
username2.mysite.com
You know, the parameter is actually the username, and its placed before the domain name. Other sites do this I have seen, for example, Wordpress Blogs and so on. Can I do this using Django URLs? Any help is appreciated.
I cannot think of a way to do this using Django's URLs because this is a subdomain NOT a URL parameter. What I would suggest is reading the subdomain in your server config (e.g nginx site config) and writing it into a header to be read by some custome middleware in django
I accomplished something like this with the "django-dynamicsites" plugin.
https://bitbucket.org/uysrc/django-dynamicsites
It's quite old and may need a few tweaks for modern Djangos but it does do what you need, I use it myself and it works well.
It involves modifying the django_sites table so doesn't play too nice with makemigrations. You may have to use the SQL to update the SQL database on your server to get around that.
I stumbled up django-subdomains which does exactly what I want. Thanks everyone.
I have built my first Django App! It is built to help my business track inventory. As such, I would not like it to be publicly available.
Maybe someday I will set up multiple user accounts, etc, but for now I really just need a basic password gate to get it up and running.
Does anyone have any middleware that works for this? All the solutions that I am finding are pretty old and they do not seem to work with the latest version of Django.
If you just need a single username/password couple, handling it directly via HTTP authentication in your webserver configuration will be the easiest way to achieve this. The benefits of this approach are:
You can set it up in 5 minutes: example with nginx, example with apache
You don't have to write code you'll delete later
It will protect all your website, including static files, third-party apps, admin, etc.
I found an answer that worked for me posted here:
#login_required for multiple views
Make sure the LOGIN_REQUIRED_URLS_EXCEPTIONS path is correctly set to your login page.
According to this StackOverflow post: DjangoRestFramework - How do I customize the frontend?
it says "Typically you want to put your DjangoRestFramework REST Api in something like /api". I'm not sure what this means, so I did some more browsing.
In this GitHub project: https://github.com/kevinastone/django-api-rest-and-angular there is an /example/ folder and inside the folder there is a folder called /api/ which has api.py and serializers.py. I've never come across a tutorial which ever told me to create an api.py file and to place an /api/ folder inside the app (it looks like the app is called "example" in the GitHub project).
I've watched this tutorial: https://godjango.com/41-start-your-api-django-rest-framework-part-1/ and it seems as if the instructor created an app called "api" which has serializers.py and urls.py. With that said, different tutorials show different things. What's the preferred way of creating a Django app which uses the DjangoRestFramework for a ReSTful API?
My end goal is to create a Django app with DjangoRestFramework and AngularJS on the frontend.
I'm sure you've already figured this out, but for other people that stumble upon this he's including his api as it's own django 'app', just like in the tutorial you mentioned. His file "api.py" is basically, for all intents and purposes, serving as what most tutorials call "views.py". But as it is where he declares the methods for his api, and not the applications views, calling it "api.py" is clear cut and makes sense. Doesn't matter what you call it so long as it's imported and used appropriately. The best way to make a rest framework is to use paths that are reflexive of their use case. 'api/post_images' for instance, would post an image and not post a comment or retrieve a user's profile information.
If you only have one rest api call, you may not be interested in holding your single api method in it's own designated file. But as your application grows the use this convention will prove its value.
I've been trying out Ruby on Rails and really like the RESTful approach for the URL's.
Now I'm trying to learn Django and I want to create the same kind of nested URL's as I did with Rails.
Lets say I wanna do this: /categories/12/products/13
This is what I've came up with, but does not seem to work:
url(r'^categories/(?P<category_id>\d+)/products/(?P<product_id>\d+/$)', 'my_app.views.product', name="product"),
How should this be done? Also, how could you use the url template helper for creating this kind of link?
Check out TastyPie. It's a drop-in REST Api for Django similar to what you're used to in Rails and will get you going in the right direction without too much work.
Glad you got it sorted, as TastyPie was mentioned I would also recommend Django REST framework also.
In a template you can use Link title
To create a url in your python code you can use reverse:
from django.core.urlresolvers import reverse
url = reverse('product', args=[category_id, product_id])
I am farily new to the topic, but I am trying to combine both Django and Pyjamas. What would be the smart way to combine the two? I am not asking about communication, but rather about the logical part.
Should I just put all the Pyjamas generated JS in the base of the domain, say http://www.mysite.com/something and setup Django on a subdirectory, or even subdomain, so all the JSON calls will go for http://something.mysite.com/something ?
As far as I understand now in such combination theres not much point to create views in Django?
Is there some solution for clean urls in Pyjamas, or that should be solved on some other level? How? Is it a standard way to pass some arguments as GET parameteres in a clean url while calling a Pyjamas generated JS?
You should take a look at the good Django With Pyjamas Howto.
I've managed to get the following to work, but it's not ideal. Full disclosure: I haven't figured out how to use the django's template system to get stuff into the pyjamas UI elements, and I have not confirmed that this setup works with django's authentication system. The only thing I've confirmed is that this gets the pyjamas-generated page to show up. Here's what I did.
Put the main .html file generated by pyjamas in django's "templates" directory and serve it from your project the way you'd serve any other template.
Put everything else in django's "static" files directory.
Make the following changes to the main .html file generated by pyjamas: in the head section find the meta element with name="pygwt:module" and change the content="..." attribute to content="/static/..." where "/static/" is the static page URL path you've configured in django; in the body section find the script element with src="bootstrap.js" and replace the attribute with src="/static/bootstrap.js".
You need to make these edits manually each time you regenerate the files with pyjamas. There appears to be no way to tell pyjamas to use a specific URL prefix when generating together its output. Oh well, pyjamas' coolness makes up for a lot.
acid, I'm not sure this is as much an answer as you would hope but I've been looking for the same answers as you have.
As far as I can see the most practical way to do it is with an Apache server serving Pyjamas output and Django being used as simply a service API for JSONrpc calls and such.
On a side note I am starting to wonder if Django is even the best option for this considering using it simply for this feature is not utilizing most of it's functionality.
The issue so far as I have found with using Django to serve Pyjamas output as Django Views/Templates is that Pyjamas loads as such
Main html page loads "bootstrap.js" and depending on the browser used bootstrap.js will load the appropriate app page. Even if you appropriately setup the static file links using the Django templating language to reference and load "bootstrap.js", I can't seem to do the same for bootstrap.js referencing each individual app page.
This leaves me sad since I do so love the "cruftless URLS" feature of Django.