Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
URL Regex in python:
import re
url_regex = re.compile(
r'^(?:http)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)

is_valid_url = url_regex.match(input_url)
#python #regex #re #url
In the last tutorial we designed a project. Project holds the whole infrastructure, it can contain multiple apps. A weblog, an eshop page, poll app could all be an app in our newly created project. The examples are endless.

After project creation to create an app you can use startapp. For now we create a poll app:
$ python manage.py startapp polls
$


The structure of the polls app should be as below:
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

All the poll application logic resides here.

We try first by creating a view that return a sample http response. For writing view for your polls application you have to put it inside of views.py:
from django.http import HttpResponse


def index(request):
return HttpResponse("Hello, world. You're at the polls index.")


Yes! That's it. We have created view in django. But for calling this view from outside you have to map a route to it. In my app I don't have the file urls.py so create it in polls and map as below:
from django.conf.urls import url

from . import views

urlpatterns = [
url(r'^$', views.index, name='index'),
]

url is django module to implement the routing functionality.

The next step is to bind the newly defined route in polls to your project urls route. So for now go to saturn/urls.py:
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]

The first url says that give every route that starts with polls to polls.urls. You can see that we have used include to reference URL configuration. When it reaches /polls it removes polls and send the remaining of the URL to polls.

Now run your project with the command below and see the result:
$ python manage.py runserver


The final step is to open your browser and point it to /polls and see the result:
http://127.0.0.1:8000/polls/


Voila! Good job everybody :)

If you go to base path you will see the error below (see the picture):

#django #url #include #views #pattern #django_part3
urlparse library is used in python in order to well, parse a URL.

Consider the example below:
from urlparse import urlparse
google_url = urlparse('https://www.google.com/profile?active=true#comment')

google_url is now of type ParseResult class, which includes all data we need:

The protocol used in URL is achievable by:
print url.scheme
'https'

Absolute url of the google url is retrieved by netloc:
print url.netloc
'www.google.com'

The path section of the URL which is after the domain section is:
print url.path
'/profile'

Query section of the URL:
print url.query
'active=true'

The fragment part of url is stored in fragment attribute:
print url.fragment
'comment'

#python #urlparse #url