Project introduction

This project is a set of web sites based on Python web class library Django developed as a public welfare project.

Preview the address

i.mypython.me/

The source address

Github.com/geeeeeeeek/…

Start the project

django-admin startproject mask
Copy the code

Create an

python3 manage.py startapp app
Copy the code

The model design

It is mainly to design the demand table Product. In this project, we need fields such as title, contact person and telephone number. Refer to the models.py file.

The design fields are as follows:

class Product(models.Model):
    list_display = ("title"."type"."location")
    title = models.CharField(max_length=100,blank=True, null=True)
    type = models.IntegerField(default=0)
    pv = models.IntegerField(default=0)
    contact = models.CharField(max_length=10,blank=True, null=True)
    location = models.CharField(max_length=20,blank=True, null=True)
    phone = models.CharField(max_length=13, blank=True, null=True)
    weixin = models.CharField(max_length=50, blank=True, null=True)
    status = models.BooleanField(default=False)
    timestamp = models.DateTimeField(auto_now_add=True, null=True)
    expire = models.IntegerField(default=1)
Copy the code

Business writing

This project is divided into three pages, namely, the list page, the details page and the submission page.

Let’s go through them all

Home page

The first is the home page, which has a templates in templates/app/index.html and is mainly used to display the content of the home page and submit search terms to the search interface, all of which is in app/urls.py, as follows

app_name = 'app'
urlpatterns = [
    path('index', views.IndexView.as_view(), name='index'),
    path('detail/<int:pk>', views.DetailView.as_view(), name='detail'),
    path('commit', views.CommitView.as_view(), name='commit')]Copy the code

We set the route of the home page to IndexView and started writing IndexView code. The code is very simple:


class IndexView(generic.ListView):
    model = Product
    template_name = 'app/index.html'
    context_object_name = 'product_list'
    paginate_by = 15
    c = None

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        paginator = context.get('paginator')
        page = context.get('page_obj')
        page_list = get_page_list(paginator, page)
        context['c'] = self.c
        context['page_list'] = page_list
        return context

    def get_queryset(self):
        self.c = self.request.GET.get("c".None)
        if self.c:
            return Product.objects.filter(type=self.c).order_by('-timestamp')
        else:
            return Product.objects.filter(status=0).order_by('-timestamp')

Copy the code

Details page

Let’s develop the detail page again. As seen in urls.py, the detail page is implemented by DetailView, so let’s get a peek at it in full:


class DetailView(generic.DetailView):
    model = Product
    template_name = 'app/detail.html'

    def get_object(self, queryset=None):
        obj = super().get_object()
        return obj

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        return context
Copy the code

It is simple, inheriting the DetailView generic template class to display details.

Submit page

Finally, look at the commit page, which is implemented by CommitView. Also watch the code:

class CommitView(generic.CreateView):

    model = Product
    form_class = CommitForm
    template_name = 'app/commit.html'

    @ratelimit(key='ip', rate='2/m')
    def post(self, request, *args, **kwargs):
        was_limited = getattr(request, 'limited'.False)
        if was_limited:
            messages.warning(self.request, "Operation too frequent, please try again in 1 minute.")
            return render(request, 'app/commit.html', {'form': CommitForm()})
        return super().post(request, *args, **kwargs)

    def get_success_url(self):
        messages.success(self.request, "Successful launch! ")
        return reverse('app:commit')
Copy the code

It inherits from CreateView, because it’s a create operation. In POST, we limit the number of commits by ratelimit to prevent malicious commits.

Run the project

python3 manage.py runserver
Copy the code

The interface display