Do not know how to implement this MVC pattern - python

I have a JS module (a bunch of interface code). Let's call it mymodule. It is located at myapp/modules/mymodule. I want to keep together urls.py and views.py associated with this module. In other words I want to somehow reference myapp/modules/mymodule/urls.py from my root myapp/urls.py and not to cram my root myapp/views.py with all those handlers associated with mymodule. So, this is what I tried by now:
## myapp/urls.py
from django.conf.urls import url
from myapp import views
urlpatters = [
url(r'^$', views.index, name = 'index'),
... a lot of other urls
... and now I want to include urls associated with mymodule
url(r'^mymodule/',include('myapp.modules.mymodule.urls'))
]
## myapp/modules/mymodule/urls.py
from myapp.modules.mymodule import handlers
urlpatters = [
url(r'^dosomething/',handlers.dosomething)
]
## myapp/modules/mymodule/handlers.py
from django.http import HttpResponse
def dosomething(request):
return HttpResponse("Hello world!")
And I want this - when a user goes to 127.0.0.1:8000/myapp/mymodule/dosomething, he or she should see "Hello world!" message.
EDIT
My updated version of the code which still leads to Page not found error looks like this:
## myapp/urls.py
from django.conf.urls import *
from myapp import views
urlpatterns = patterns('',
url(r'^$',views.index, name='index'),
... a lot of other working urls
url(r'^myapp/mymodule',include('myapp.modules.mymodule.urls')),
)
## myapp/modules/mymodule/urls.py
from django.conf.urls import *
from myapp.modules.mymodule import handlers
urlpatterns = patterns('',
url(r'^dosomething/',handlers.dosomething),
)
## myapp/modules/mymodule/handlers.py
from django.http import HttpResponse
def dosomething(request):
return HttpResponse("Hello world!")
EDIT
My root urls.py file now looks like this:
## myapp/urls.py
from django.conf.urls import *
from myapp import views
urlpatterns = patterns('',
url(r'^$',views.index, name='index'),
... a lot of other working urls
url(r'^myapp/mymodule/',include('myapp.modules.mymodule.urls')),
)
So, there is an ending slash now, but the problem is - when I go to 127.0.0.1:8000/myapp/mymodule/dosomething, the Django debugger says that Page not found and that it tried just ^myapp/ ^myapp/mymodule/ pattern - so I see no mentioning of ^dosomething/ pattern. However, if I now go to 127.0.0.1:8000/myapp/myapp/mymodule/dosomething, I now see in the debugger page that it tried ^myapp/ ^myapp/mymodule/ /dosomething pattern. It says /dosomething, because I played with mymodule/urls.py and now it looks like:
## myapp/modules/mymodule/urls.py
from django.conf.urls import *
from myapp.modules.mymodule import handlers
urlpatterns = patterns('',
url(r'/dosomething',handlers.dosomething),
)
So, I guess the reason of all these troubles is that I can't combine all these patterns together to make them work. I need clear understanding on how my url pattern should look like inside root urls.py and inside myapp/urls.py

Add the myapp prefix to the url regex:
url(r'^myapp/mymodule/',include('myapp.modules.mymodule.urls'))
Also note that urlpatters should be created by the patterns() function:
from django.conf.urls import patterns
urlpatterns = patterns('',
...
)
UPDATE: Sorry, I didn't notice that first urls.py is from the app. I confused it with the project urls.py. So as I understand now you should have three urls.py:
project/urls.py
urlpatterns = patterns('',
url(r'^myapp/',include('myapp.urls')),
)
myapp/urls.py
urlpatterns = patterns('',
url(r'^mymodule/',include('myapp.modules.mymodule.urls')),
)
myapp/modules/mymodule/urls.py
urlpatterns = patterns('',
url(r'dosomething/', handlers.dosomething),
)

Related

How does Django know to call the index?

I just started learning Django and I am a bit confused. I'm following the official documentation and this is the code:
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
polls/views.py
from django.http import HttpResponse
from django.conf import settings
from django.shortcuts import render
from django.views import View
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I am mainly confused that if I add more functions in polls/views.py, how will using inclde('polls.urls') know that I wish to call the index function or some other function?
Link for the documentation page that I am following: https://docs.djangoproject.com/en/3.2/intro/tutorial01/
Essentially it constructs a list of paths (strings) that it tries to build up and then find which you wanted
so paths in polls for "" and foobar would lead django to try to match up to the following urls
polls/
polls/foobar
The full path for the views.index is 'polls/' + '' and thus 'polls/', so it appends the two.
If you make an extra path:
# polls/urls.py
urlpatterns = [
path('', views.index, name='index')
path('foo/', views.other_view, name='other-view')
]
then that path pattern will be 'polls/'+'foo/' = 'polls/foo/', so it will disambiguate between the two.
If you thus visit polls/, then the first path will match and thus "fire" index. If you visit polls/foo, then the other-view will "fire".

Django Reverse function not working in this example

I'm not able to access the url named "forum" using the reverse function. It gives me this error:
django.urls.exceptions.NoReverseMatch: Reverse for 'forum' not found. 'forum' is not a valid view function or pattern name.
I'm able to access all my other namespaces URLs within the other apps. But I just can't access anything by name from my root URLs.py file.
Here is my root URLs.py file
from django.contrib import admin
from django.urls import path, include
from machina import urls as machina_urls
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('forum/',include(machina_urls), name='forum'),
path('admin/', admin.site.urls),
path('symposium/', include('symposium.urls')),
path('elearning/', include('elearning.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Remove:
from machina import urls as machina_urls
Change the url to this:
path('forum/', include(machina.urls),),
in the urls.py file of machina add above the urlpatterns:
app_name = 'machina'
If you are going to use reverse remember:
reverse('app_name:url_name')
For example we have the following url in our machina.urls:
path('bitcoin/', views.bitcoin, name='bitcoin'),
Your reverse would look like this:
reverse('machina:bitcoin')
docs: https://docs.djangoproject.com/en/3.1/ref/urls/#include << Class based overview
Examples: https://docs.djangoproject.com/en/3.1/topics/http/urls/#namespaces-and-include << Copy and write overview
Other option is to have a separate file with url pattern lists for example:
url_list.py
from django.urls import path
from yesyes import views
varvar = [
path('', views.testbased, name='index'),
]
urls.py:
from django.urls import path, include
from yesyes.url_list import varvar as urllist
urlpatterns = [
path('admin/', admin.site.urls),
path('test/',include(urllist), name='test'),
]

After linking urls&views "hello world" is not visible in django

i have just now started learning the django basics ,but i have been keeping on facing a problem with the urls & views. i do not get the "Hello world" after i reload or even try adding myapp name in the url
wrote this code for the project url
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('calc.urls')),
path('admin/', admin.site.urls),
]
wrote this for myapp urls
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
wrote this for the views
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("Hello world");
what is the mistake i am doing?
link: http://127.0.0.1:8000/calc
You are not setting the correct URL. Also have you added your in installed apps ?
if yes then visit: http://127.0.0.1:8000
or add calc in your url to use http://127.0.0.1:8000/calc
urlpatterns = [
path('calc', views.home, name='home'),
]
Is your myapp urls file inside a folder called calc? If it isn't then try doing something like this:
Change include('calc.urls') to include('\the folder name in which myapp urls is in\.urls')
Then use the http://127.0.0.1:8000 and it should work.

Django URL patterns not being read

So I am following a Django tutorial and I have reached the stage where I add my own URL patterns to the urls.py file.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url((r'^webapp/', include('webapp.urls'))),
url(r'^admin/', admin.site.urls),
]
the problem is that when I run the server Django doesnt even search for the pattern. I'm not sure what I'm doing wrong here
webapp.urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Make sure the directory structure is:
app
app
urls.py
webapp
urls.py
You write about a file named webapp.urls.py that is unusable by django
You pass wrong arguments for url().
Wrong:
url((r'^webapp/', include('webapp.urls')))
Right:
url(r'^webapp/', include('webapp.urls'))

Django import doesnt work when trying to import views

I have the following urls.py file:
from django.conf.urls import patterns, url
from base import views
urlpatterns = patterns('',
url(r'^$', 'views.index', name='index'),
url(r'^item/new', 'views.newItem', name='newItem'),
url(r'^item/submitted', 'views.itemSubmitted', name='itemSubmitted'),
)
This doesnt work, it gives me an ImportError message saying that there is no module named views. When i remove the second import line above and change the lines from views.viewname to base.views.viewname it works. Does someone know why the import is not working?
Your url route list statement is using string statements to define the location of the views. Django will attempt to lazy-load view methods when required, which can be great for strange situations where importing the view methods would cause import loops. If import loops aren't a problem (which they shouldn't be), you have two ways of doing this:
from django.conf.urls import patterns, url
from base import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^item/new', views.newItem, name='newItem'),
url(r'^item/submitted', views.itemSubmitted, name='itemSubmitted'),
)
or
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^$', 'base.views.index', name='index'),
url(r'^item/new', 'base.views.newItem', name='newItem'),
url(r'^item/submitted', 'base.views.itemSubmitted', name='itemSubmitted'),
)
In the former, you are passing the view method as a property for the route. In the latter, you are passing an import path to the view methods. Note that in the latter you do not need to provide an import statement for the view.
To cut down on repitition, you can also extract the repeated prefix 'base.views':
from django.conf.urls import patterns, url
urlpatterns = patterns('base.views',
url(r'^$', 'index', name='index'),
url(r'^item/new', 'newItem', name='newItem'),
url(r'^item/submitted', 'itemSubmitted', name='itemSubmitted'),
)

Categories