小编Zul*_*tra的帖子

Django URL 在根和包含的 url 之间给我空间

所以我用这行在 root/project/urls.py 中创建了一个 url

from django.conf.urls import include
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('users/', include('app.urls'))
]
Run Code Online (Sandbox Code Playgroud)

在我的 root/app/urls.py 中

from django.urls import path

from .views import UserView, AuthenticationView

urlpatterns = [
    path('register/', UserView.as_view()),
    path('auth/', AuthenticationView.as_view()),
]
Run Code Online (Sandbox Code Playgroud)

所以预计会给我http://localhost:8000/users/registerhttp://localhost:8000/users/auth网址。

同时,我的请求没有按预期运行。

在此处输入图片说明

显然它在根路径和包含路径之间返回了一个空格。我检查了我的 root/project/settings.py 文件我没有发现任何奇怪的设置。有人知道发生了什么吗?

python django url

5
推荐指数
1
解决办法
1891
查看次数

如何在Swift中对私有或内部函数进行单元测试?

因此,我创建了一个自定义抽象类,该抽象类继承自UIViewController(由RebloodViewController继承)名为MainViewController的类。在这一节课中,我写了一个可重用的笔尖注册函数

class MainViewController: RebloodViewController {

    typealias Cell = RebloodViewController.Constants.Cell

    internal func registerNib(_ cellNib: Cell.Nib, target: UICollectionView) {

        let nib = UINib(nibName: cellNib.rawValue, bundle: nil)

        do {
            let identifier = try getCellIdentifierByNib(cellNib)
            target.register(nib, forCellWithReuseIdentifier: identifier)
        } catch {
            fatalError("Cell identifier not found from given nib")
        }
    }

    private func getCellIdentifierByNib(_ nib: Cell.Nib) throws -> String {

        var identifier: String? = nil

        switch nib {
        case .articles:
            identifier = Cell.Identifier.articles.rawValue
        case .events:
            identifier = Cell.Identifier.events.rawValue
        }

        guard let CellIdentifier = identifier else …
Run Code Online (Sandbox Code Playgroud)

unit-testing nib ios swift

4
推荐指数
1
解决办法
1621
查看次数

返回十六进制 UUID 作为 Django 模型字符域的默认值

我试图用从 uuid4 生成的标识符创建一个模型。但是我想要的不是常规 uuid,而是标识符具有十六进制 uuid 格式(不带“-”)。这是我尝试过的:

class Model(models.Model):

    identifier = models.CharField(max_length=32, primary_key=True, default=uuid.uuid4().hex, editable=False)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.identifier

    class Meta:
        abstract = True
Run Code Online (Sandbox Code Playgroud)

不是每次继承类实例化时都返回唯一的 id,而是返回相同的 id,因为uuid4(). 我试图将默认值从 to 更改uuid.uuid4().hexuuid.uuid4.hex但似乎hex不能uuid4直接调用。那么从十六进制格式的 uuid 为我的标识符生成默认值的可能方法是什么?

python django uuid hex

3
推荐指数
1
解决办法
2464
查看次数

Django account_activation_token.check_token 在 constant_time_compare() 中总是返回 False

我有一个ActivationTokenGenerator创建将用于帐户验证的令牌,该令牌将通过电子邮件发送。例如,我使用时间戳、ID 和用户活动状态等参数对其进行了配置:

from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six


    class ActivationTokenGenerator(PasswordResetTokenGenerator):

        def _make_hash_value(self, user, timestamp):
            return six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.is_active)


    account_activation_token = ActivationTokenGenerator()
Run Code Online (Sandbox Code Playgroud)

然后我使用account_activation_token用于在我发送的验证电子邮件中生成令牌send_mail

@classmethod
    def send_email(cls, request, user):
        current_site = get_current_site(request)
        mail_subject = 'Activate your Poros account.'
        message = render_to_string('email_verification.html', {
            'user': user,
            'domain': current_site.domain,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
            'token': account_activation_token.make_token(user),
        })
        to_email = user.email
        email = EmailMessage(
            mail_subject, message, to=[to_email]
        )
        email.send()
Run Code Online (Sandbox Code Playgroud)

一切看起来都很完美的电子邮件发送的令牌包含在一个 url 中,其模式如下:

url(r'activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
        activate, name='activate'),
Run Code Online (Sandbox Code Playgroud)

在电子邮件中看起来像这样:

http://{{ domain }}{% …
Run Code Online (Sandbox Code Playgroud)

python django token

3
推荐指数
1
解决办法
3216
查看次数

React Router 中的中间 URL 参数

我正在尝试路由一条带有中间参数的路径。这是我的路线的样子。

import Events from "./views/EventsView";
import Event from "./views/EventView";
import NotFound from "./views/NotFoundView";
import RegistrationView from "./views/RegistrationView"

export default [
  {path: "/", component: Events, exact: true},
  {path: "/events", component: Events, exact: true},
  {path: "/events/:projectId", component: Event, exact: true},
  {path: "/events/:projectId/register", component: RegistrationView, exact: true},
  {component: NotFound}
]
Run Code Online (Sandbox Code Playgroud)

然后我将我的路线映射到 a <Switch>which 中 a 中<Router>

<Switch>
    {
        return routes.map(({path, component, exact}, key) =>
            <Route key={key} path={path} component={component} exact={exact}/>)
}
</Switch>
Run Code Online (Sandbox Code Playgroud)

Link用来访问Event组件。

<Link to="/events/1234">Title Here</Link>
Run Code Online (Sandbox Code Playgroud)

问题 …

javascript reactjs react-router react-router-v4 react-router-dom

1
推荐指数
1
解决办法
1524
查看次数