Django views and urls - can't runserver - django

I'm learning django and having issues with implementing this code below:
from django.urls import path
from . import views
urlpatterns = [
path('members/', views.members, name='members'),
]
The code above is stored in a urls.py file I created in an app folder as directed by the tutorial guide. However when I try to run it on the browser I get this message:
The included URLconf '<module 'members.urls' from 'C:\Users\Chinasaokwu\Desktop\storefront\try-django\my_app\members\urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.
I don't know what to do here. I'm new to django.
It's w3schools.com that I am using as a guide
I'm still learning how views and urls work since I'm new to django but I understand that views are functions that take and return https requests/response, and that urls are used to implement views.

Related

First view in DJANGO tutorial -> Page not found (404)

I was doing tutorial "Writing your first Django app, part 1" and I got stuck in "Write your first view". I have fulfil all instructions regardings files. When I go to http://127.0.0.1:8000/ then I can see "The install worked successfully! Congratulations!" but if I go to http://localhost:8000/polls/ then I see:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/polls/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn’t match any of these.
In this case according instructions I should see "Hello, world. You're at the polls index". I attached 2 files content and 2 interesting screens (why does it emphasize?).
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
I was looking answer in Stack Overflow (also in hints) or YouTube but unfortunately without success. Interesting but in another computer it works, but on my laptop not.
So you are doing the URLS incorrectly, or partially at least.
You need to create another app in django called polls:
python manage.py startapp polls
Then in the polls/urls.py file, include an extension for /polls, that references your view in polls/view.py:
urlpatterns = [
path('/polls', views.<view-name>, name='Polls URL')
]
Then it should work, otherwise you are doing something wrong and need to elaborate on our problem.
Note that Django's DEBUG should be True when developing at all times, so you can see the full error log. DEBUG=FALSE is used if you have a webapp public (aka deployed/in production.
Subject closed. Solution -> Bad configuration urls.py file in mysite and polls. Check content both files in tutorial context.

Loading a .html file in django url with parameters

I have a file which I have to load directly through the web browser but it would still run inside of my django app.
What I have done so far is
url(r"^file_name.html", views.regular_person, name='regular_person')
But when I do this it shows the page could not be found. error 404
Please how can i get that to work.
Thanks
edit
the file would also have some query parameters
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('foo/', TemplateView.as_view(template_name='filename.html'))
]
django documentation
Is this something you are looking for.

Django URL order

iam stuck again I hope I will get useful help this time too.
Iam trying to run the app but it gives me URL configuration error something like this:
Using the URLconf defined in pyshop.urls, Django tried these URL patterns, in this order:
admin/
products/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
I see a similar query in this platform but I couldn't find my answer
Iam using Django version 2.1
My code in pyshop.urls is:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
]
You don't route / anywhere, and
The empty path
implies that's exactly where you're visiting.
You'll either need to
add e.g. path('', SomeHomepageViewMaybe),
or remove the /products/ prefix to make the products app a "main" app (not that there's a concept like that in Django),
or just navigate to /products/ instead

Template URL redirection generating ImportError "no module named" error in Django 1.6

I'm trying to migrate my application from Django 1.0 to 1.6 and am running into a problem rendering templates with URL redirections.
My application structure is:
mysite
settings.py
urls.py
myapp
urls.py
views.py
etc.
myapp.urls contains:
from mysite.myapp import views
urlpatterns = patterns('',
# Login/Logout/Registration
url(r'^login/$', views.login, name="login"),
url(r'^registration/$', views.registration, name="registration"),
)
When I browse to /login, the URL is mapped correctly and invokes the view. However, when the view attempts to render its template, it generates this error:
ImportError at /login
No module named myapp
and points to this redirection syntax in the template:
<a href="{% url 'registration' %}">
I can browse to /registration without errors, so it has something to do with resolving back to the URL.
It must be something simple, but I'm stumped. Any suggestions would be greatly appreciated.
Finally tracked down the problem. What I had coded was correct. However, there were some other url patterns at the bottom of my url.py file that I had not converted to 1.6. For example
url(r'/schedule/$', 'schedule_list'),
And when the URL resolver looked for a reverse match, it apparently looped through the url patterns starting at the bottom and errored immediately. I guess I assumed it would either start at the top of the url patterns or there was some sort of lookup table that mapped names back to urls.
Project layout was changed in django 1.4.
Delete mysite part of the module name in myapp/urls.py. It should be:
from myapp import views

Why are the Django project URLs not all available to the Django test client?

I've been trying to add the django-lean app to my project.
The django-lean app is not located in the project I'm working on, it is on the PYTHONPATH.
I have not been able to get the django-lean tests to pass.
It seems the issue is that the TestCase defines a value for urls:
urls = 'django_lean.experiments.tests.urls'
As best as I can tell, the tests are only getting the urls located # 'django_lean.experiments.tests.urls', but not
the urls from the rest of the project.
This is causing error messages like:
NoReverseMatch: Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.
These are triggered by {% url %} template tags in the project.
How can I make sure that the all the urls of the project are available for the tests?
EDIT:
Someone showed me a script to print the visible URLs:
import urls
def show_urls(urllist, depth=0):
for entry in urllist:
print " " * depth, entry.regex.pattern
if hasattr(entry, 'url_patterns'):
show_urls(entry.url_patterns, depth + 1)
I called this script from ipdb, this was the output:
ipdb> import urls
ipdb> show_urls(urls.urlpatterns)
^test-experiment/(?P<experiment_name>.*)$
^test-clientsideexperiment/(?P<experiment_name>.*)$
^admin/
^(?P<experiment_name>.+)/$
^$
^main-app/
^goal/(?P<goal_name>.*)$
^confirm_human/$
This corresponds to the urls located # 'django_lean.experiments.tests.urls'
urlpatterns = patterns('django_lean.experiments.tests.views',
url(r'^test-experiment/(?P<experiment_name>.*)$', 'experiment_test'),
url(r'^test-clientsideexperiment/(?P<experiment_name>.*)$', 'clientsideexperiment_test'))
urlpatterns += patterns('',
url(r'^admin/', include('django_lean.experiments.admin_urls')),
url(r'^main-app/', include('django_lean.experiments.urls')),
The issue that I'm having is that my tests all fail because of the named urls from other apps in the project are called by URL template tags are not accessible to the tests.
I'm running Python 2.7 with Django 1.2.1
The solution was pretty simple. Just import the URLs from the main project into the urls.py for the app.
from forum.urls import urlpatterns
or for a more generic solution:
from settings import ROOT_URLCONF as project_urls
urlpatterns = __import__('forum.urls').urls.urlpatterns
To list all the url patterns your django knows you can use the answer suggested here.
Run this from your tests and print/log the output.
Just note that its better to explicitly state where to import the urls from like
from myproject import urls
because you probably have some other modules containing urls files.